Four factorial frames stack up and pop off in reverse, while fib(4) branches into a tree of repeat calls.
The big picture
Meet recursion
A function that calls itself sounds like a trap — with a base case it becomes the cleanest tool in the box, powering everything from file trees to maze solvers.
1
A function calls itself
factorial(4) waits on factorial(3), which waits on factorial(2) — each call shrinks n by 1.
2
Base case first
The exit ramp: factorial stops at n == 1. Without it the calls never end.
3
The call stack
Every call pushes a frame; every return pops one. Python allows ~1000 frames before RecursionError.
4
Backtracking
Recurse on a choice, then undo it — picked.append(x) explores, picked.pop() backtracks.
5
The hidden bill
Naive fib(4) calls itself 9 times because fib(2) is computed twice — overlapping subproblems.
Meet recursion
A function calls itself1factorial(4) waits on factorial(3), which waits on factorial(2) — each call shrinks n by 1.
Base case first2The exit ramp: factorial stops at n == 1. Without it the calls never end.
The call stack3Every call pushes a frame; every return pops one. Python allows ~1000 frames before RecursionError.
Backtracking4Recurse on a choice, then undo it — picked.append(x) explores, picked.pop() backtracks.
The hidden bill5Naive fib(4) calls itself 9 times because fib(2) is computed twice — overlapping subproblems.
5 building blocks make up Meet recursion
Algorithm in motion
Call stack live
factorial(4) — watch four frames stack up, then pop off in exactly reverse order.
main()
top → main()
1 / 10
main() owns the first frame. It calls factorial(4) — watch frames stack up, then pop off in reverse.
Visualize
Backtrack loop
Every backtracking search is this loop: choose, explore, undo — until the goal or the dead end.
process decision input / output start / end
Watch it run
Walkthrough
factorial prints as the stack unwinds — then subsets() shows the append / recurse / pop backtrack rhythm.
factorial_trace.py
1deffactorial(n):
2print(f"enter factorial({n})")
3if n ==1:
4print("base case: 1! is 1")
5return1
6 smaller =factorial(n -1)
7 result = n * smaller
8print(f"factorial({n}) = {n} x {smaller} = {result}")
9return result
10
11answer =factorial(4)
12print(f"4! = {answer}")
factorial(4)
answer = factorial(4) — a frame opens with n = 4.
Memory
n= 4
Console
output appears as the program runs…
Step 1/15
Algorithm in motion
Fibonacci fan-out
fib(4) — one boss call fans into nine calls, and fib(2) does all its work twice.
fib(4)
1 / 12
fib(4) = fib(3) + fib(2) — the boss call fans out, left child first. Watch the same subtrees re-run.
Challenge
Predict the output
Same recursion, but the print sits on the other side of the call.
predict.py
1defmystery(n):
2if n ==0:
3return
4mystery(n -1)
5print(n)
6
7mystery(3)
Your predicted output — one line per console line
Hint: The print happens AFTER the recursive call returns — each frame only prints once the one below it is done, so the smallest n comes first.
Practice
Concept check
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand
?base casedrops into place
?call stack framedrops into place
?RecursionErrordrops into place
?backtrackingdrops into place
?fib(2) twicedrops into place
?unwinddrops into place
?infinite recursiondrops into place
?2ⁿ subsetsdrops into place
Recap
Key takeaways
Key takeaways6 takeaways
01
Every recursive function needs a base case — it is the only thing that stops the calls.
02
Each call pushes a frame on the call stack; each return pops one — LIFO, like everything recursive.
03
Python caps the stack near 1000 frames: deeper recursion raises RecursionError.
04
Print before the recursive call and output descends; print after it and output ascends.
05
Backtracking= recursion plus an undo step: append, explore, pop, try the next choice.