Chapter 10: Functions
Functions let you organize your code, reuse it, and make your program clean and easy to understand.
Think of a function as a small machine:
- You give it some input (parameters)
- It does some work
- It gives you back a result (return value)
1. What is a Function?
A function is a block of code that performs a specific task and has a name. Instead of writing the same code many times, you write it once in a function and call it whenever needed.
2. Defining and Calling Functions
Defining a function (writing the function):
|
0 1 2 3 4 5 6 7 8 9 |
return_type function_name(parameters) { // code to execute return value; // if it returns something } |
Calling a function (using it):
|
0 1 2 3 4 5 6 |
function_name(arguments); |
Simple Example – Function without parameters and without return
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> // Function definition void sayHello() { printf("Hello! Welcome to C programming!\n"); } int main() { // Calling the function sayHello(); // Prints the message sayHello(); // You can call it many times sayHello(); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 |
Hello! Welcome to C programming! Hello! Welcome to C programming! Hello! Welcome to C programming! |
3. Function Parameters and Return Values
Parameters = Inputs the function receives Return value = Output the function gives back
Example – Function with parameters and return value
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> // Function that takes two integers and returns their sum int add(int a, int b) { // a and b are parameters int sum = a + b; return sum; // returns the result } int main() { int x = 10, y = 20; int result; result = add(x, y); // Calling function and storing return value printf("Sum of %d and %d is %d\n", x, y, result); return 0; } |
Output:
|
0 1 2 3 4 5 6 |
Sum of 10 and 20 is 30 |
4. Types of Functions
There are 4 main combinations:
A. No arguments, no return value (like sayHello above)
B. Arguments but no return value
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
void printMessage(char msg[]) { printf("Message: %s\n", msg); } int main() { printMessage("Good Morning!"); return 0; } |
C. No arguments but returns a value
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int getLuckyNumber() { return 7; // Always returns 7 } int main() { int num = getLuckyNumber(); printf("Your lucky number is %d\n", num); return 0; } |
D. Arguments and return value (most common and useful)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
float calculateArea(float length, float width) { return length * width; } int main() { float l = 5.5, w = 4.2; float area = calculateArea(l, w); printf("Area of rectangle = %.2f\n", area); return 0; } |
Output:
|
0 1 2 3 4 5 6 |
Area of rectangle = 23.10 |
5. Recursion
A function that calls itself is called recursive.
Example – Factorial using recursion
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> int factorial(int n) { if (n == 0 || n == 1) { return 1; // Base case (stops recursion) } return n * factorial(n - 1); // Recursive call } int main() { int num = 5; printf("Factorial of %d = %d\n", num, factorial(num)); return 0; } |
How it works (n=5):
- 5 * factorial(4)
- 5 * 4 * factorial(3)
- 5 * 4 * 3 * factorial(2)
- 5 * 4 * 3 * 2 * factorial(1)
- 5 * 4 * 3 * 2 * 1 = 120
Output:
|
0 1 2 3 4 5 6 |
Factorial of 5 = 120 |
Note: Always have a base case (stopping condition), otherwise infinite recursion → crash!
6. Scope and Storage Classes
Scope = Where a variable can be used.
A. Local variables (declared inside function) – only visible inside that function B. Global variables (declared outside all functions) – visible everywhere
Storage Classes – Control how long and where variables exist:
| Storage Class | Keyword | Lifetime | Default Value | Scope |
|---|---|---|---|---|
| auto | (default) | Until function ends | Garbage | Inside function |
| static | static | Whole program (but only one copy) | 0 | Inside function (local) or global |
| extern | extern | Whole program | 0 | Global (declared elsewhere) |
| register | register | Until function ends | Garbage | Inside function (try to store in CPU register) |
Example – static variable (keeps value between calls)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> void counter() { static int count = 0; // Only initialized once count++; printf("Function called %d times\n", count); } int main() { counter(); // 1 counter(); // 2 counter(); // 3 return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 |
Function called 1 times Function called 2 times Function called 3 times |
7. Function Prototypes (Declaration)
If you call a function before defining it, compiler needs to know about it.
Prototype = Tells compiler the function’s name, parameters, and return type.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Function prototype (declaration) int multiply(int x, int y); // Later in code - definition int multiply(int x, int y) { return x * y; } int main() { int res = multiply(5, 6); printf("Result = %d\n", res); return 0; } |
Best Practice: Always put prototypes at the top of the file (after includes).
Today’s Homework
- Write a function greet() that prints “Good Morning!” and call it from main.
- Write a function maxOfTwo(int a, int b) that returns the larger of two numbers.
- Create a function isEven(int n) that returns 1 if even, 0 if odd.
- Write a recursive function to calculate the sum of first N natural numbers.
- Create a program with a static variable inside a function that counts how many times the function is called.
