Nearby lessons

46 of 72

C - Functions Overview

Functions Overview is one of the foundational topics in C programming. This lesson explains Why Functions?, The Three Parts of a Function and Arguments and Return Values with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Why Functions?

A function is a named block of code that does one task. Instead of writing one giant main(), we split the work into small functions. Benefits:

  • Reusability — write once, use many times.
  • Easy to read — each task has a clear name.
  • Easy to debug — fix one small function, not the whole program.
  • Team work — different people write different functions.
In simple words: a function is a small helper you hire — you give it data, it does one job, and (if you ask) it hands a result back. Instead of one giant main(), you hire several small helpers.

The Three Parts of a Function

PartWhat it isExample
Function declarationTells C the function exists (before main)int add(int, int);
Function definitionThe actual body of the functionint add(int a, int b) { return a + b; }
Function callUsing the function in mainresult = add(5, 3);
Example02
CCode Cell
1#include <stdio.h>
2 
3int add(int, int); // 1. declaration
4 
5void main()
6{
7 int s = add(5, 3); // 3. call
8 printf("Sum = %d\n", s);
9}
10 
11int add(int a, int b) // 2. definition
12{
13 return a + b;
14}
Output
Sum = 8

Arguments and Return Values

A function can receive values (arguments) and/or return a value back. This gives four combinations:

TypeArgumentsReturn valueExample
1YesYesint add(int a, int b) — returns a + b
2YesNovoid show(int n) — just prints
3NoYesint getNum() — just returns a value
4NoNovoid welcome() — just does something
📝 Key Takeaways
  • Functions split code into reusable, readable tasks.
  • Three parts: declaration, definition, call.
  • Four combinations of arguments and return values.
  • C uses call by value — functions get a copy, originals stay unchanged.
  • return gives a value back and ends the function.
  • Recursion = a function calling itself, with a base case to stop.
  • Classic recursion: factorial and Fibonacci.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4