5.1 Function Basics: Declaration, Definition, Call
A function groups statements under a name so they can be reused. Its declaration (prototype) tells the compiler the function's name, return type, and parameter types ahead of use. Its definition supplies the actual body. A call transfers control to the function; when it hits return (or its closing brace, for void functions), control returns to the statement right after the call.
5.2 Parameter Passing: Call by Value vs Call by Reference
C is strictly call-by-value: when you pass a normal variable, the function receives a COPY, so changes inside the function never affect the caller's original variable. To let a function modify the caller's data, you pass the variable's address (a pointer) — this is how C simulates 'call by reference', and it's the mechanism scanf() relies on.
5.3 Recursion
A recursive function calls itself to solve a smaller instance of the same problem, combined with a base case that stops the recursion. Each call gets its own stack frame with its own copies of parameters and local variables; results 'unwind' back up the call chain as each call returns. Recursion is elegant for problems with a natural self-similar structure (factorial, Fibonacci, tree traversal) but costs stack memory proportional to recursion depth.
int fact(int n) {
if (n == 0) return 1; // base case
return n * fact(n - 1); // recursive case
}
5.4 Storage Classes: auto, static, extern, register
Storage classes control a variable's scope, lifetime, and default value. auto is the default for local variables (block-scoped, destroyed when the block ends). static gives a local variable a lifetime spanning the whole program (it retains its value between calls) or, for a global, restricts its visibility to the current file. extern declares that a variable/function is defined in another file, enabling sharing across translation units. register suggests (not guarantees) that a variable be kept in a CPU register for faster access.
| Storage Class | Storage Location | Default Initial Value | Scope | Lifetime |
|---|---|---|---|---|
auto | Stack Frame | Garbage / Undefined | Local block | Function execution |
static | Data / BSS Segment | Zero (0) | Local block or Current File | Entire program lifetime |
extern | Data / BSS Segment | Zero (0) | Global / Multi-file | Entire program lifetime |
register | CPU Register | Garbage / Undefined | Local block | Function execution |