Nearby lessons

70 of 124

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
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
CCode Cell
1#include <stdio.h>
2 
3void welcome(); // no args, no return
4int getNumber(); // no args, returns int
5void showSum(int a, int b); // args, no return
6int add(int a, int b); // args, returns int
7 
8void main()
9{
10 int x = getNumber();
11 showSum(x, 10);
12 printf("add returns: %d\n", add(x, 5));
13}
14 
15void welcome() { printf("Welcome\n"); }
16int getNumber() { return 42; }
17void showSum(int a, int b) { printf("Sum = %d\n", a + b); }
18int add(int a, int b) { return a + b; }
📝 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