The Call Stack
Step 1 of 9
frames0order outnewest first
call stack
walk(3)↩ leave 3
walk(2)↩ leave 2
walk(1)↩ leave 1
walk(…)
walk(…)
output
enter 3
enter 2
enter 1
leave 1
leave 2
leave 3
Every call gets a frame: its own arguments, and the line to come back to when the call it made finally returns.
The Idea
Every call gets its own frame holding its arguments and the line to resume on. Frames pile up as the recursion goes deeper, then pop off in reverse as calls return. Stack too deep and Python stops you with a RecursionError.
Real-World Example
Phone calls on hold. You pause your sister to take a work call, then pause that for the courier at the door. The courier clears first, then work, then your sister — the newest interruption is always the first one finished.
The Code
Your turn
Put the steps in the right order.
- The walk(1) frame pops and walk(2) resumes
- The walk(2) frame is pushed
- The walk(2) frame pops and walk(3) resumes
- The walk(1) frame is pushed
Mini quiz
1 / 3