The short answer
The base case is the condition in a recursive function that stops the calls. When it applies, the function returns a value directly, without calling itself again. Every other call moves the problem closer to that case.
The two parts of a recursive function
A recursive function has two parts:
- The base case: a simple input whose answer is known without recursion.
- The recursive case: a call to the same function with a smaller or simpler input.
Here is a function that adds the whole numbers from n down to 0:
#include <iostream>
int sumTo(int n) {
if (n == 0) { // base case
return 0;
}
return n + sumTo(n - 1); // recursive case
}
int main() {
std::cout << sumTo(4) << "\n"; // 4 + 3 + 2 + 1 + 0 = 10
return 0;
}
The base case is the simplest input, 0, because the sum of no numbers is 0. Each recursive call adds n to the sum of the numbers below it.
What happens without a base case
Consider a function that always makes another call:
int neverStops(int n) {
return neverStops(n + 1); // no base case
}
Each call waits for the next one, so the calls pile up. Without optimization, the stack eventually runs out of space and the program crashes. With optimization, a compiler may turn the calls into an endless loop. Either way, the program never finishes, and the fix is always the same: add a base case that the input can reach.
Choosing a base case
A good base case meets two tests:
- It is the simplest input the function can answer without recursion.
- Every starting input eventually reaches it, because each recursive call moves closer.
Off-by-one errors break the second test. This version only stops at 1, so it fails when it starts at 0:
int factorialWrong(int n) {
if (n == 1) { // only 1 is checked
return 1;
}
return n * factorialWrong(n - 1);
}
Calling factorialWrong(0) asks for factorialWrong(-1), then factorialWrong(-2), and so on. The value 1 is never reached, so the calls never stop. Checking n <= 1 instead handles both 0 and 1.
Functions with more than one base case
Some functions need more than one base case. The Fibonacci sequence, in its common definition, starts with 0 and 1, and each later number is the sum of the two before it. A recursive version needs both starting values as base cases:
int fib(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fib(n - 1) + fib(n - 2);
}
Each call makes two smaller calls, so both starting values must stop the recursion. Without the n == 1 check, fib(1) would call fib(0) and fib(-1), and the negative inputs would never reach a base case.
Tips for writing base cases
- Start with the smallest input, such as 0 or 1, and work out its answer by hand.
- Put the base case first in the function body, so it is easy to spot.
- Test the function with inputs that reach the base case, including the edge cases.
- If the output looks wrong, trace the calls and confirm that each one moves toward the base case.
Summary
A base case is the stopping point of a recursive function. It returns a known answer for the simplest input, and every input the function accepts must be able to reach it. A recursive function in C is only as reliable as its base case, whether it sums numbers, computes factorials or builds a Fibonacci sequence.