Watch factorial(4) stack four frames and unwind to 24 — then trace fibonacci’s tree as it recomputes the same values.
The big picture
Why recursion works
Recursion solves a problem by calling itself on a smaller copy — the call stack does the bookkeeping, and backtracking reuses the trick to explore choices.
1
A function calls itself
factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2)… every call works on a strictly smaller input.
2
The base case
factorial(1) returns 1 and stops the descent. No base case means frames pile up forever — stack overflow.
3
Every call gets a frame
The call stack hands each call its own private n. factorial(4) stacks 4 frames before the first one returns.
4
Unwinding
On the way back up, frames pop and results combine: 2 * 1 = 2, then 3 * 2 = 6, then 4 * 6 = 24.
5
Backtracking
Recurse into a choice, undo it, try the next — the engine behind subsets, permutations and maze solving.
Why recursion works
A function calls itself1factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2)… every call works on a strictly smaller input.
The base case2factorial(1) returns 1 and stops the descent. No base case means frames pile up forever — stack overflow.
Every call gets a frame3The call stack hands each call its own private n. factorial(4) stacks 4 frames before the first one returns.
Unwinding4On the way back up, frames pop and results combine: 2 * 1 = 2, then 3 * 2 = 6, then 4 * 6 = 24.
Backtracking5Recurse into a choice, undo it, try the next — the engine behind subsets, permutations and maze solving.
5 building blocks make up Why recursion works
Algorithm in motion
Call stack unwind
factorial(4) pushes four frames, each with its own n — then the stack unwinds back to one answer.
empty — top is -1
1 / 11
main() is running and the stack is empty. Calling factorial(4) is about to pile frames up.
Visualize
Anatomy of a call
One recursive call is a round trip: descend, wait for the smaller answer, combine, pop.
process decision input / output start / end
Watch it run
Walkthrough
Factorial stacking up and down — then binary digits, where printing happens during the unwind.
factorial.cpp
1#include<iostream>
2usingnamespacestd;
3
4longlongfactorial(int n){
5if(n <=1)return1;
6return n *factorial(n -1);
7}
8
9intmain(){
10cout<<factorial(4)<<endl;
11return0;
12}
factorial(4)
main calls factorial(4) — a frame opens with its own n = 4.
Memory
n= 4
Console
output appears as the program runs…
Step 1/13
Algorithm in motion
Recursion tree
fib(4) branches into a tree of 9 calls — and the same fib(2) is computed twice from scratch.
fib(4)
1 / 12
fib(4) is called. For n > 1 it must split into fib(n-1) + fib(n-2) — watch the tree grow call by call.
Challenge
Predict the output
Two prints per call — one on the way down, one on the way up.
predict.cpp
1#include<iostream>
2usingnamespacestd;
3voidtrace(int n){
4if(n ==0){cout<<0;return;}
5cout<< n;
6trace(n -1);
7cout<< n;
8}
9intmain(){
10trace(3);
11cout<<endl;
12return0;
13}
Your predicted output — one line per console line
Hint: The first cout runs on the way DOWN (3, 2, 1, then 0 at the base case); the second runs on the way back UP (1, 2, 3).
Practice
Concept check
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand
?base casedrops into place
?recursive casedrops into place
?call framedrops into place
?stack overflowdrops into place
2*1=2
?backtrackingdrops into place
?fib(4) callsdrops into place
?binary(13)drops into place
Recap
Key takeaways
Key takeaways6 takeaways
01
Every recursion needs a base case — factorial stops at n ≤ 1; without it the stack overflows.
02
Each call gets its own frame with its own n — four live calls means four n values in memory.
03
Code after the recursive call runs during the unwinding — that is how binary(13) prints 1101 in order.
04
The stack grows on the way down and shrinks on the way up — the deepest call always finishes first.
05
Backtracking= recurse, undo, try again — the engine behind subsets, permutations and mazes.
06
Naive fibonacci recomputes the same subtree again and again — fib(10) makes 177 calls.