Functions in C
As programs grow, writing everything inside main() becomes messy. Imagine a 500-line main() where the same calculation is copied 10 times.
A function is a named block of code that does one job. You write it once and call it whenever you need it. In this lesson you will learn what functions are, why we use them, and how to write your first one.
What You Will Learn
- What a function is.
- Why we use functions (advantages).
- Library functions vs user-defined functions.
- The parts of a function: name, parameters, return type, body.
- How calling a function works.
What is a Function in C?
Exam definition: A function is a self-contained block of statements that performs a specific task. It can take input values (parameters) and can give back a result (return value). Every C program has at least one function: main().
Memory trick: A function is like a juicer machine: you put fruit in (arguments), it does its job (body), and juice comes out (return value). You don't rebuild the machine every time; you just use (call) it.
Why do we use functions?
- Reusability: write once, call many times.
- Less repetition: no copy-pasting the same code.
- Easy to read: a big program is split into small, named parts.
- Easy to test and fix: a bug is found in one small function, not in 500 lines.
- Teamwork: different people can write different functions.
Types of functions
| Type | Written by | Examples |
|---|---|---|
| Library (built-in) functions | Already written, come with C | printf(), scanf(), sqrt(), strlen() |
| User-defined functions | You, the programmer | area(), is_even(), show_menu() |
Parts of a function
| Part | Meaning | In int add(int a, int b) |
|---|---|---|
| Return type | Type of value it gives back (void = nothing) | int |
| Name | What you call it | add |
| Parameters | Inputs it receives | int a, int b |
| Body | The statements inside { } | { return a + b; } |
Syntax of a Function in C
return_type function_name(parameter_list)
{
/* body */
return value; /* not needed if return_type is void */
}
To use it, call it by name: result = function_name(arguments);
Example 1: Your First User-Defined Function
#include <stdio.h>
int add(int a, int b) /* function definition */
{
return a + b;
}
int main(void)
{
int x = add(10, 20); /* function call */
printf("10 + 20 = %d\n", x);
printf("5 + 7 = %d\n", add(5, 7));
return 0;
}
Output
10 + 20 = 30 5 + 7 = 12
Code Explanation
int add(int a, int b): a function namedaddthat takes two ints and returns an int.return a + b;: sends the sum back to whoever called it.add(10, 20): calls the function. 10 goes intoa, 20 intob. It returns 30, which is stored inx.add(5, 7): the same function is reused with different values; its result goes straight into printf.
How a Function Call Works
main()reachesadd(10, 20)and pauses.- The values 10 and 20 are copied into the parameters
aandb. - The body of
addruns. returnsends the result back, andmain()continues from where it paused.- The values you pass are called arguments (actual parameters); the variables that receive them are parameters (formal parameters).
Example 2: Reusing a Function (Area of Three Circles)
#include <stdio.h>
float circle_area(float r)
{
return 3.14159f * r * r;
}
int main(void)
{
printf("Radius 1: area = %.2f\n", circle_area(1.0f));
printf("Radius 2: area = %.2f\n", circle_area(2.0f));
printf("Radius 5: area = %.2f\n", circle_area(5.0f));
return 0;
}
Output
Radius 1: area = 3.14 Radius 2: area = 12.57 Radius 5: area = 78.54
Without a function, the formula would be written three times. With a function, it is written once, and if it ever needs a change, you fix it in one place.
Important Points to Remember
- A function is a block of code that does one specific task.
- Two types: library (built-in) and user-defined.
- Parts: return type, name, parameters, body.
- Every program has
main(), where execution starts. - Main benefit: reusability.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| Calling a function before it is defined or declared | Error or warning: implicit declaration | Define it above main() or add a prototype |
Missing return in a non-void function | Garbage return value | Always return a value |
| Writing a function inside another function | Error: nested functions are not standard C | Write each function separately |
| Wrong number of arguments | Error: too few/many arguments | Match the parameter list |
Functions in C Practice Questions
- What is a function? Give two advantages.
- What is the difference between library and user-defined functions?
- Name the four parts of a function.
- What is the difference between arguments and parameters?
- What does the return type
voidmean?
Show answers
- A block of code that does a specific task. Advantages: reusability, easier to read and debug.
- Library functions come with C (printf, sqrt); user-defined functions are written by the programmer.
- Return type, name, parameters, body.
- Arguments are the values passed in the call; parameters are the variables in the definition that receive them.
- The function returns no value.
Coding Exercise
Write a function int cube(int n) that returns n × n × n. Call it from main() for 2, 3 and 4.
Show solution
#include <stdio.h>
int cube(int n)
{
return n * n * n;
}
int main(void)
{
printf("2^3 = %d\n", cube(2));
printf("3^3 = %d\n", cube(3));
printf("4^3 = %d\n", cube(4));
return 0;
}
2^3 = 8 3^3 = 27 4^3 = 64
Quick Quiz: Functions in C (MCQ)
-
Which function must every C program have?
A) printf() B) main() C) start() D) scanf()
-
sqrt()is a:A) User-defined function B) Library function C) Keyword D) Operator
-
In
float area(float r), the return type is:A) area B) r C) float D) void
-
The main advantage of functions is:
A) Slower code B) Reusability C) More memory D) More typing
-
Values passed when calling a function are called:
A) Parameters B) Arguments C) Returns D) Keywords
Show answers
- B: main().
- B: a library function.
- C: float.
- B: reusability.
- B: arguments (actual parameters).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Function | Block of code for one specific task |
|---|---|
| Types | Library (built-in), User-defined |
| Parts | Return type, name, parameters, body |
| Compulsory function | main() |
| Arguments | Values passed in the call (actual) |
| Parameters | Variables in the definition (formal) |
| void return | Returns nothing |
Frequently asked questions
- What are the advantages of functions?
- Reusability, less repetition, easier reading, easier debugging and teamwork.
- What is the difference between actual and formal parameters?
- Actual parameters are the values passed in the call; formal parameters are the variables in the function definition.
- Can a function be defined inside another function in C?
- No, standard C does not allow nested function definitions.
- Is main() a user-defined function?
- Yes: its body is written by you, but its name and role are fixed by C.
Remember
- Function = juicer machine: fruit in, juice out.
- Write once, call many times.
Next Topic
Next lesson: Function Declaration, Definition and Call. You will learn the three steps of using a function: declaration, definition and call.