01 / 20

Tracing Recursion
in C++

Master the art of tracing recursive calls step by step, understand the call stack, and build intuition for how functions call themselves.

C++ Computer Science

What Is Recursion?

A recursive function is a function that calls itself as part of its own execution. Every recursive function must have two essential parts: a base case that stops the recursion, and a recursive case that breaks the problem into a smaller sub-problem.

int factorial(int n) { if (n <= 1) return 1; // base case return n * factorial(n - 1); // recursive case }

The Call Stack

The call stack is a region of memory that keeps track of active function calls. When a function is called, a new frame is pushed onto the stack. When it returns, that frame is popped off. The stack follows a Last In, First Out (LIFO) order.

main() factorial(3) factorial(2) factorial(1) ← top push pop LIFO: Last In, First Out

Why Tracing Matters

Tracing recursion means manually following each function call and return value step by step. It builds intuition about the order of operations, helps you predict output, and is essential for debugging recursive code.

Predict Output
Know exactly what a recursive function will print or return for any input.
Debug Errors
Spot infinite recursion, wrong base cases, or incorrect return values.
Exam Ready
AP CS and coding interviews frequently test hand-tracing of recursive functions.

The Two Rules of Recursion

1
Base Case: A condition that stops the recursion. Without it, the function calls itself forever until a stack overflow occurs.
2
Recursive Case: The function calls itself with a smaller or simpler argument, making progress toward the base case with each call.

Anatomy of a Recursive Call

factorial(4) 4 * factorial(3) factorial(3) 3 * factorial(2) factorial(2) 2 * factorial(1) factorial(1) = 1 returns 1 returns 2 returns 6 returns 24 BASE CASE INITIAL CALL Final result: 24

Iteration vs Recursion

Iteration (Loops) Recursion
Mechanism Repeats via for/while Calls itself
Termination Loop condition becomes false Base case is reached
Memory Single frame on the stack One frame per call
Risk Infinite loop Stack overflow
Readability Familiar, straightforward Elegant for tree/divide problems

How to Trace: Step by Step

1
Write the initial call with its argument value.
2
Check the base case. If met, write the return value. If not, proceed.
3
Expand the recursive call. Write out the new call with its smaller argument. Indent to show depth.
4
Repeat steps 2-3 until you hit the base case.
5
Unwind: substitute return values back up the chain, computing each frame's result.

Example 1: Tracing sumTo(4)

int sumTo(int n) { if (n == 0) return 0; return n + sumTo(n - 1); }

Example 2: Fibonacci Tree

int fib(int n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); }
fib(4) fib(3) fib(2) fib(2) fib(1)=1 fib(1)=1 fib(0)=0 fib(1)=1 fib(0)=0 fib(4) = 3 (9 total calls!)

Example 3: Print Before vs After

// Print BEFORE recursion void countDown(int n) { if (n < 1) return; cout << n << " "; countDown(n-1); }
// Print AFTER recursion void countUp(int n) { if (n < 1) return; countUp(n-1); cout << n << " "; }

Stack Unwinding Visualized

When countUp(3) is called, the print statement comes after the recursive call. The function reaches the base case first, then unwinds back up, printing at each return.

WINDING DOWN countUp(3) countUp(2) countUp(1) countUp(0) → return UNWINDING returns, prints: 1 returns, prints: 2 returns, prints: 3 Output: 1 2 3

Multiple Recursive Calls

Some functions make more than one recursive call per invocation. This creates a tree of calls instead of a linear chain. Each branch is resolved independently before the caller can combine results.

Linear Recursion
One recursive call per invocation. Creates a simple chain: factorial, sumTo, power.
Tree Recursion
Two or more recursive calls. Creates a branching tree: Fibonacci, merge sort, binary tree traversal.

Tracing Return Values

int power(int base, int exp) { if (exp == 0) return 1; return base * power(base, exp - 1); }

Common Mistakes

✕ Missing Base Case
Without a stopping condition, the function recurses infinitely until a stack overflow crash.
✕ No Progress Toward Base
If the argument doesn't change or moves away from the base case, recursion will never terminate.
✕ Ignoring Return Value
Calling \(f(n-1)\) without return means the computed value is lost. Each frame must pass its result back.

Recursion Cheat Sheet

Concept Key Point Example
Base Case Stops recursion if (\(n \le 1\)) return 1
Recursive Case Calls itself smaller \(n \cdot f(n-1)\)
Call Stack LIFO frame storage Each call = new frame
Print Before Output on the way down 3 2 1
Print After Output on the way up 1 2 3
Tree Recursion Multiple calls per frame \(\text{fib}(n-1) + \text{fib}(n-2)\)

Question 1 of 5

What is the output of the following code?

Question 2 of 5

How many times is fib() called in total when computing fib(5)?

Question 3 of 5

What is the output of this code?

Question 4 of 5

What happens if a recursive function has no base case?

Question 5 of 5

What does the following function return when called as mystery(4)?