Functions in C
Breaking a program into reusable, organized blocks of code.
What is a Function?
A function is a named block of code that performs a specific task, which you can call (reuse) from anywhere in your program instead of repeating code. It has a return type, a name, parameters (inputs), and a body.
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3); // calling the function
printf("%d", result);
}Call by Value vs Call by Reference
By default, C passes arguments 'by value' — the function gets a COPY of the argument, so changes inside the function don't affect the original variable. To actually modify the original, you must pass a pointer to it ('call by reference' using &).
void increment(int *x) {
(*x)++; // modifies the ORIGINAL variable
}Why Use Functions?
Functions make code reusable (write once, call many times), organized (each function does one clear job), and easier to debug (test each function independently).
🌍 Real-World Use
A large software project (like a banking app) might have thousands of functions — each handling one small job like 'validateEmail()' or 'calculateInterest()' — making the huge codebase manageable and testable piece by piece.
💡 Pro Tip
If you need a function to modify a value in the calling code (not just return one new value), you MUST pass a pointer — this single concept (call by reference) trips up almost every C beginner at least once.
🧪 Quick Self-Test
Check what you just learned — no pressure, just practice.
1. By default, how does C pass arguments to a function?
2. How do you let a function modify the caller's original variable?