Sub topic 3
What Happens on the Call Stack
This chapter assumes you can write and call functions, and that you have seen a recursive function with a base case and a recursive case. It explains the mechanism that lets a function call itself without losing track of its own work.
Frames: one per call
A call stack is a region of memory where the runtime records the function calls that are currently active. Each call gets its own stack frame, which holds:
- the arguments passed to that call
- the local variables created inside it
- the return address, which is the point in the caller to resume once this call finishes
The stack is last-in, first-out. A call pushes a new frame on top. A return pops the top frame and passes its value to the frame below it. Only the top frame is running. Every frame beneath it is paused, waiting for the call it made to come back.
Recursion uses exactly this mechanism. When factorial(3) calls factorial(2), the runtime pushes a separate frame for the new call. Both frames have a variable named n, but each frame's n belongs to that frame alone, so they never overwrite each other.
Watching frames appear
The inspect module can count how many frames are on the stack at any moment. The code below prints that count as the recursion goes down and as it comes back up.
import inspect
def factorial(n):
depth = len(inspect.stack())
print(f"{' ' * n}push factorial({n}), stack depth {depth}")
if n == 0:
result = 1
else:
result = n * factorial(n - 1)
print(f"{' ' * n}pop factorial({n}) -> {result}")
return result
factorial(3)The absolute depth numbers depend on where you run the code, because the interpreter and the module level take up frames too. What matters is the pattern. Depth goes up by one on each call. The base case runs at the deepest point. The returns then happen in reverse order. factorial(3) started first and finishes last.
Why the pending work matters
In n * factorial(n - 1), the multiplication can't run until the inner call returns. The frame has to keep n alive until that happens. This is the cost of recursion: a recursive call that still has work left to do keeps its frame on the stack, so a recursion that goes d levels deep holds d frames in memory at once.
A loop doesn't work this way. A for loop computing a factorial updates the same result variable inside a single frame, so its stack usage stays the same however many times it repeats.
| Aspect | Recursive call | Loop iteration |
|---|---|---|
| New frame per step | Yes | No |
| Where state lives | Arguments and locals of each frame | Variables updated in one frame |
| Stack memory | Grows with depth | Constant |
| Order of completion | Last call finishes first | Steps finish in sequence |
Common misconceptions
- "All the calls share one
n." They don't. Each frame has its own copy. - "The function finishes when it reaches the base case." Only the deepest call finishes then. Every caller above it still has pending work to complete as the stack unwinds.
- "A return jumps back to the first call." A return goes to the frame directly below, which is the caller that made that call.
Exercise
While computing factorial(4) with the recursive function above, how many factorial frames are on the stack at the moment the base case runs, and which frame is popped last?
Show answer
Five factorial frames are on the stack: factorial(4), factorial(3), factorial(2), factorial(1) and factorial(0). The frame for factorial(4) is popped last, because it was pushed first and its multiplication waits for every inner call to return.
Exercise
Why does a for loop computing factorial(1000) use constant stack space, while the recursive version needs about 1000 frames?
Show answer
The loop updates its variables inside a single frame on each pass. Each recursive call pushes a new frame, and that frame has to stay on the stack until its inner call returns so it can finish its multiplication.