Nearby lessons
70 of 124C - 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
| Part | What it is | Example |
|---|---|---|
| Function declaration | Tells C the function exists (before main) | int add(int, int); |
| Function definition | The actual body of the function | int add(int a, int b) { return a + b; } |
| Function call | Using the function in main | result = add(5, 3); |
Example02
Arguments and Return Values
A function can receive values (arguments) and/or return a value back. This gives four combinations:
| Type | Arguments | Return value | Example |
|---|---|---|---|
| 1 | Yes | Yes | int add(int a, int b) — returns a + b |
| 2 | Yes | No | void show(int n) — just prints |
| 3 | No | Yes | int getNum() — just returns a value |
| 4 | No | No | void welcome() — just does something |
In simple words: arguments are the message you send to the function; the return value is the reply it sends back. No message (no args) and no reply (void) are both perfectly fine.
Example03
📝 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 QuestionsProgress: 0 / 4