Why put work in a function?
The key idea
A function gives a piece of work a name and a defined interface. It can receive values through parameters and return a result. This lets you reason about a small task without keeping the entire program in your head.
The most useful starting question is the contract: what inputs are accepted, what result is returned, and what other effects occur? A function name alone is not enough to answer those questions.
Read and test the example
#include <stdio.h>
int maximum(int a, int b) {
if (a > b) {
return a;
}
return b;
}
int main(void) {
printf("%d\n", maximum(-3, -8));
return 0;
}
The output is -3. The function receives two integers and returns the greater value. If they are equal, returning either input produces the same numeric result. No addition or multiplication is needed, so this comparison avoids arithmetic overflow.
| First value | Second value | Expected return |
|---|---|---|
| 4 | 9 | 9 |
| -3 | -8 | -3 |
| 5 | 5 | 5 |
The function calculates a value; main decides how to display it. Keeping those roles separate makes the function easier to reuse in another context.
Parameters are local values
In this example, a and b are local parameter values. Assigning a different value to a inside the function would not directly change the caller's original integer variable. C passes arguments by value.
You can pass a pointer value when a function needs to work with an object through its address. That adds requirements about valid storage and lifetime, which are covered in the pointers guide. Do not use a pointer merely to avoid understanding return values.
Return a result deliberately
The declared return type describes the kind of result the caller can receive. Make sure every relevant execution path in a non-void function returns an appropriate value.
A return statement ends that function call; it does not necessarily end the entire program. The caller resumes after the call and can use the returned result in another expression.
Choose a small responsibility
Compare a function that finds the greater of two values with one that reads input, prints a menu, changes several variables and writes a file. The second has more assumptions and effects to understand.
For beginner practice, keep one clear calculation together. Separate input handling and display unless they are the specific subject you are studying. Use names that communicate the operation and write down any restrictions.
Your practice task
Write a function that returns the smaller of two integers. Test the same three categories: different positive values, different negative values and equal values. Explain how the comparison changed.
Then use the original maximum function twice to find the largest of three fixed integers. The exercise shows how small functions combine without repeating their internal logic. It is a practical stepping stone toward larger C programs and later C++ study.
References and course details
Use these references for further study. The examples and exercises in this article are educational; consult the catalogue for course details.