Functions and recursion.
Understand prototypes, calls by value, return types, and recursive state.
The idea behind the syntax.
A function declaration gives the compiler a name, return type, and parameter types before a call is checked. A definition also supplies the body. In a call, argument expressions are evaluated and their values become parameter values; C passes parameters by value. If a parameter is a pointer, its copied value still points to the caller's object, so the function may modify that object when its type allows it. That is different from changing the caller's pointer variable itself.
A function's return value can communicate a calculated result, a status, or a pointer with a clear lifetime. C functions have one direct return value, but a struct can package several related results. A void return type means no value is produced. A prototype using (void) explicitly says that the function takes no arguments; an empty parameter list has historical meaning and is best avoided in portable teaching code.
What C guarantees.
Recursion is a function calling itself on a smaller or simpler problem. It needs a base case that does not recurse and a step that moves toward that base case. Each call has its own automatic local objects, and deep recursion consumes stack space. For a small bounded tree or mathematical definition, recursion can make the structure clear; an iterative form may be safer when depth depends on untrusted input.
A good function boundary states what it reads, what it changes, and what it returns. const pointer parameters make read-only access visible, while pointer-plus-length pairs state the accessible region. The example computes a factorial for a deliberately small nonnegative input, with a return type large enough for that chosen value. It illustrates the call sequence without claiming that factorial fits for arbitrary n.
Why the detail matters.
Call by value means the callee receives parameter objects initialized from argument values. If the parameter is an int, assigning to it changes only that local object. If it is an int *, the copied pointer still identifies the caller's int, and dereferencing it can change the caller's object. That is why a pointer parameter is often described informally as an output parameter, though the actual C call mechanism has not changed. Keep the distinction visible in the function's contract.
Recursive calls create a chain of pending work. In factorial, each call waits for the smaller call's result before multiplying. The base case stops the chain, and return values flow back upward. The chain's depth grows with n, so a mathematically valid recurrence is not automatically a safe implementation for arbitrary input. A production routine should define its accepted range and detect arithmetic overflow before trusting the result.
See the rule in context.
#include <stdio.h>
unsigned long factorial(unsigned int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main(void) {
unsigned int n = 5;
printf("%u! = %lu\n", n, factorial(n));
return 0;
}factorial(5) multiplies 5 by factorial(4), which continues down to factorial(1). The base case returns 1; the calls then return 2, 6, 24, and 120 as the stack unwinds. printf's %u matches n and %lu matches the unsigned long result. The output is 5! = 120. The parameter n is a separate value in each call.
Where the rule stops.
Unsigned arithmetic can wrap when the mathematical factorial exceeds the type's range. The example is valid for n = 5, but a general factorial API needs an explicit maximum or an overflow check.
Keep this model.
Functions make behavior reusable, but names also need a lifetime and visibility. The next chapter distinguishes a local automatic variable from a static object and explains how declarations in headers connect separate source files.