📘 Section 5 — Functions
Learn how to define reusable code blocks, pass data through parameters, and build modular programs.
🎯 What You’ll Learn
- What functions are and why we use them
- How to define and call functions
- The difference between formal and actual parameters
- How to use return values
- Calling functions with call by value
1. What Is a Function?
A function is a block of code which only runs when it is called. Data can be passed to the function and this data is known as parameters. Define the code once and use it many times.
2. Your First Function
Here’s the simplest example — a function that prints a message:
#include <stdio.h>
void fun() {
printf("hi");
}
int main() {
fun();
return 0;
}
| Part | Meaning |
|---|---|
void |
The function does not return a value |
fun |
The name of the function |
() |
No parameters are needed |
fun(); |
This is how we call (execute) the function |
3. Functions with Parameters
You can pass data to a function using parameters:
#include <stdio.h>
void fun(int a) { // formal parameters
printf("%d", a);
}
int main() {
int a = 5;
fun(a); // actual parameters
return 0;
}
Formal vs Actual Parameters
| Term | Where? | Description |
|---|---|---|
| Formal parameter | In the function definition | The variable name used inside the function |
| Actual parameter | At the call site | The value or variable you pass to the function |
4. Calling Methods
| Method | Description |
|---|---|
| Call by Value | A copy of the value is passed. The original variable is unchanged. |
| Call by Reference | The memory address is passed (using pointers). Changes affect the original. |
📌 In this section we focus on Call by Value. You’ll learn Call by Reference when we cover Pointers.
5. Functions with Return Values
Some functions only do an action, like printing. These functions can be void.
Other functions calculate a value and send it back to the place where the function was called. These functions use return.
#include <stdio.h>
int square(int x) {
return x * x;
}
int main() {
int result = square(5);
printf("%d", result);
return 0;
}
Here, square(5) returns 25, and this value is stored in result.
returnends the function and sends a value back to the caller.
🧪 Exercises — Trace the Output
Practice reading code carefully. Try to predict the output before revealing the answer!
Exercise 1: Single Parameter
void Fun(int y) {
printf("y = %d\n", y);
}
int main() {
Fun(5); // first call
int x = 6;
Fun(x); // second call
Fun(x + 1); // third call
return 0;
}
🟢 Click to Show Answer
Output:
y = 5
y = 6
y = 7
Explanation:
Fun(5)→yreceives5, printsy = 5Fun(x)→xis6, soyreceives6, printsy = 6Fun(x + 1)→x + 1=7, soyreceives7, printsy = 7
Exercise 2: Two Parameters — Watch the Order!
void Fun(int y, int x) {
printf("x = %d\ny = %d\n", x, y);
}
int main() {
int x = 25, y = 13;
printf("x = %d\ny = %d\n", x, y);
Fun(x, y);
return 0;
}
🟢 Click to Show Answer
Output:
x = 25
y = 13
x = 13
y = 25
Explanation:
This is a tricky one! Pay close attention to the order of parameters:
- In
main(), we printx = 25andy = 13— straightforward. - When we call
Fun(x, y), the value ofx(which is25) goes into the first parameter ofFun, which is namedy. The value ofy(which is13) goes into the second parameter, which is namedx. - So inside
Fun:y = 25andx = 13. - The
printfinsideFunprintsxfirst, theny— giving usx = 13,y = 25.
⚠️ Lesson: Parameter names inside a function are independent of variable names outside it. What matters is the order in which values are passed!
Exercise 3: Local Variable Scope
void Fun(int y) {
int x = 100;
printf("x = %d\ny = %d\n", x, y);
}
int main() {
int x = 25, y = 13;
printf("x = %d\ny = %d\n", x, y);
Fun(x);
return 0;
}
🟢 Click to Show Answer
Output:
x = 25
y = 13
x = 100
y = 25
Explanation:
- In
main():x = 25,y = 13→ printsx = 25andy = 13. Fun(x)is called withx = 25, soFun’s parameteryreceives25.- Inside
Fun, a local variablex = 100is created — this is completely separate frommain’sx. - So
Funprintsx = 100andy = 25.
⚠️ Lesson: Each function has its own scope. A variable named
xinsideFunis not the same asxinmain.
🛠️ Coding Exercises
Exercise 4: Hours to Seconds Converter
Task: Write a function called ToSeconds() that converts hours to seconds. The function should take in one integer parameter representing the number of hours and returns the equivalent number of seconds. Using the above function, calculate how many seconds are there in a day, week, and month.
💡 Hint
- 1 hour = 60 minutes × 60 seconds = 3600 seconds
- 1 day = 24 hours
- 1 week = 7 × 24 hours
- 1 month ≈ 30 × 24 hours
🟢 Click to Show Solution
#include <stdio.h>
int ToSeconds(int hours) {
return hours * 60 * 60;
}
int main() {
int hours = 24;
int seconds = ToSeconds(hours);
printf("%d hours = %d seconds\n", hours, seconds);
int hoursInDay = 24; // 1 day = 24 hours
int hoursInWeek = 7 * 24; // 1 week = 7 days = 7 * 24 hours
int hoursInMonth = 30 * 24; // 1 month = 30 days = 30 * 24 hours
printf("Hours in 1 day = %d\n", ToSeconds(hoursInDay));
printf("Hours in 1 week = %d\n", ToSeconds(hoursInWeek));
printf("Hours in 1 month = %d\n", ToSeconds(hoursInMonth));
return 0;
}
Key takeaway: The function ToSeconds is called multiple times with different values. This is the power of functions — write once, reuse everywhere.
Exercise 5: Quad Root Calculator
Task: Using the built-in math function sqrt(), write the function Quadroot() that returns the quad root (⁴√) of an integer.
💡 Recall: ⁴√n = √(√n) — apply the square root twice.
💡 Hint
- Include
<math.h>to usesqrt() - The 4th root of a number is the square root of its square root
- Handle the case where the input is negative (no real 4th root exists)
🟢 Click to Show Solution
#include <stdio.h>
#include <math.h> // needed for the use of sqrt function
double Quadroot(int num) {
return sqrt(sqrt(num));
}
int main() {
int num;
printf("Enter a number to calculate its quad root: ");
scanf("%d", &num);
if (num < 0) {
printf("Negative number quad root is not a real number\n");
} else {
double quadroot = Quadroot(num);
printf("Quad root = %.2f\n", quadroot);
}
return 0;
}
Example run:
Enter a number to calculate its quad root: 81
Quad root = 3.00
Why does this work?
sqrt(81)=9sqrt(9)=3- So ⁴√81 = 3 ✓
📋 Quick Reference
| Concept | Syntax |
|---|---|
| Void function (no return) | void funcName() { ... } |
| Function with return | int funcName() { return value; } |
| With parameters | void funcName(int a, int b) { ... } |
| Calling a function | funcName(arg1, arg2); |
| Using return value | int result = funcName(arg); |
⬅️ Back to Home