C Programming • C17 baseline, standards-specific features labeled
Recursion and Base Cases
Use recursion when a problem naturally reduces to a smaller instance, and make the stopping case explicit.
A recursive function calls itself with a smaller or simpler input. Each call consumes stack space, so recursion needs a reachable base case and should not be used without considering input limits. Iteration is often preferable for very deep input.
Example
int sum_to(int value) {
if (value <= 0) {
return 0;
}
return value + sum_to(value - 1);
}
What to expect
sum_to(5) returns 15.
Key idea: Prove that every recursive path approaches a base case, and check integer range and stack depth.