Recursion Call Stack¶
Run the Recursion Call Stack MicroSim Fullscreen
Edit in the p5.js Editor
About This MicroSim¶
The call stack is the canonical invisible machine: students see
factorial(4) = 24 but never the frames stacking up and unwinding. This sim
draws each frame as a physical box remembering its own n and its paused
work (4 × ?). Frames pile upward until the base case lights gold, then
pop one at a time as each pending multiplication fills in and its answer
flows down. The countdown(3) preset shows recursion with side effects and
no return value; Missing Base Case grows the tower past n = 0 until a
RecursionError banner appears.
Learning objective: The student will be able to deconstruct a recursive call into stack frames, identify the base case, and trace values returning back down the stack.
- Bloom's Taxonomy (2001): Analyze — deconstruct, organize, trace
- Interaction pattern: step-through with every frame's variables and pending work visible
How to Use¶
- Predict how tall the tower will get for
factorial(4), then step and count the frames. - Watch the turn-around moment: the gold base case is the only frame that answers without asking anyone else.
- On the way down, follow each answer: 1 → 2 → 6 → 24.
- Try countdown(3) (recursion that prints instead of returning) and Missing Base Case (why the stopping rule matters).
Iframe Embed Code¶
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan¶
Grade Level¶
Upper elementary and middle school (ages 10-14), Chapter 24 (Recursion and Fractals); also useful for tracebacks in Chapter 23
Duration¶
15 minutes
Prerequisites¶
- Functions, parameters, and return values (Chapters 4 and 11)
Activities¶
- Tower prediction (4 min): Students predict the maximum frame count for factorial(4), then factorial(6) (without the sim).
- Two-phase trace (5 min): Step through all 8 steps; students narrate the "up with pauses, down with answers" rhythm in their own words.
- Break it (4 min): Run Missing Base Case. Students write the one line that fixes it and say where it must go.
Assessment¶
- Student states the maximum stack depth for factorial(n)
- Student can identify the base case in a new recursive function
- Student can explain what RecursionError means and how to prevent it
References¶
- Python Tutorial — more on defining functions — background on function calls
- Wikipedia — Call stack — the general concept
- p5.js — the JavaScript library used to build this MicroSim