Lesson 15: Recursion — Base Cases, Recursive Calls and Tracing
Recursion is a technique in which a method calls itself to solve a smaller version of the same problem, and it is one of the most elegant ideas in programming. A recursive method has two parts: a base case, a simple input for which the answer is known directly and no further call is made, and a recursive case, which breaks the problem into a smaller piece, calls the method on that piece, and combines the result. Without a base case, the method calls itself forever until the program runs out of memory with a StackOverflowError; without progress toward the base case, the same thing happens.
Each call to a recursive method creates a new stack frame holding that call's parameters and local variables, and the frames pile up until a base case is reached, then unwind as each call returns its result to the one that called it. Tracing recursion means drawing that stack: write down the call, its argument, and what it is waiting for, then follow the calls down to the base case and the returns back up. The classic example is the factorial: factorial(n) returns 1 when n is 0, and otherwise returns n × factorial(n − 1), so factorial(3) waits for factorial(2), which waits for factorial(1), which waits for factorial(0), which returns 1, and the results multiply back up to 6.
Recursion is natural for problems with a self-similar structure: walking a linked list, exploring a tree, or searching a sorted array by halving it. It is also the natural way to express some mathematical definitions, such as the Fibonacci numbers or the greatest common divisor. But recursion is not always the best tool: a simple loop is often clearer and faster for counting or summing, and a recursive method that makes many repeated calls can be very slow. The exam asks you to trace recursion and to write simple recursive methods, not to use recursion for everything, so learn to recognize the problems it suits and the ones it does not.
Writing a recursive method starts with the base case, and the most common error is getting it wrong. Ask what the smallest input is and what the answer should be for it, and write that first, before the recursive case. Then ask how to make the problem smaller: what argument will you pass to the recursive call, and how will you use its result? Check that every possible input reaches a base case — a method that handles n = 0 but is called with negative numbers will never stop — and test it with the smallest inputs by hand before trusting it with larger ones.
The Fibonacci numbers are the classic example of recursion that looks simple but hides a trap: fib(n) returns fib(n − 1) + fib(n − 2), with base cases fib(0) = 0 and fib(1) = 1, and it recomputes the same values many times, so fib(30) makes over a million calls. That inefficiency does not make the definition wrong — it is a correct and clear statement of the sequence — but it shows that recursion is a way of expressing an idea, not always the fastest way to compute it. A loop with two running variables computes the same numbers in a fraction of the time. The lesson is to judge a recursive method on two questions: is it correct, and is it fast enough for the inputs it will face?
A worked example
Worked example: the method below sums the numbers from 1 to n recursively. Trace sum(3): it returns 3 + sum(2), sum(2) returns 2 + sum(1), sum(1) returns 1 + sum(0), and sum(0) is the base case returning 0, so the results add back up to 6.
public int sum(int n)
{
if (n <= 0)
{
return 0; // base case
}
return n + sum(n - 1); // recursive case
}
// sum(3) = 3 + sum(2) = 3 + 2 + sum(1) = 3 + 2 + 1 + sum(0) = 6
The base case is n <= 0, which returns 0, and the recursive case reduces the problem by one each time, so every non-negative input reaches the base case after n + 1 calls. If the base case were n == 0 and the method were called with −1, the calls would go −1, −2, −3, and never stop, which is why the base case uses <= rather than ==. When you trace a recursive method, write the chain of calls first, then work back up from the base case, writing each return value beside its call.
Key Terms
- Recursion
- A method calling itself on a smaller version of the same problem.
- Base case
- The simple input for which a recursive method returns directly, without another call.
- Recursive case
- The part of a recursive method that calls itself on a smaller input.
- Stack frame
- The record of one method call's parameters and variables, kept on the call stack.
- StackOverflowError
- The error when recursive calls pile up without reaching a base case.
Discussion Questions
- Why must every recursive method have a base case?
- What is a stack frame, and why does recursion need one per call?
- When is a loop a better choice than recursion?
Practice Questions
- Write a recursive method factorial that returns n! for n >= 0.
- What is the base case of the sum method in this lesson?
- Trace sum(2) step by step.
- A recursive method has no base case. What happens when it is called?
- Why is n <= 0 a safer base case than n == 0 for the sum method?
Show answers
- public int factorial(int n) { if (n <= 1) { return 1; } return n * factorial(n - 1); }
- n <= 0, returning 0.
- sum(2) = 2 + sum(1); sum(1) = 1 + sum(0); sum(0) = 0; so the result is 2 + 1 + 0 = 3.
- It calls itself forever until the call stack overflows with a StackOverflowError.
- Because a negative argument would never equal 0 and the recursion would never stop.