Skip to content
All apps

Recursion and the Call Stack

About this app

Python recursion step by step: every call gets a frame with its own n, the call stack grows and shrinks, pending multiplications wait like 4 * □, and the band of the substitution model writes along. fakultaet and fakultaetIt compared (also with tail call optimisation, which Python does not have), plus the Fibonacci call tree with and without memoization and exact call counts up to fib(100).

With explain mode: an AI tutor walks you through the app, points out what matters in the scene and answers your questions.

Every call has a frame of its own

When a function calls itself, Python creates a new frame with its own names for every call. At its deepest point, fakultaet(5) has five frames with n = 5, 4, 3, 2, 1 on the call stack at the same time, and four multiplications are waiting. The computing happens only while the stack unwinds. The tail-recursive version fakultaetIt carries its state in the arguments, but in Python it still needs one frame per call. With fib every call makes two more calls: a call tree.

What the app shows

On the left the program from the slides runs line by line, and an arrow points to the next line. In the middle the call stack grows and shrinks as a column of frame cards with bindings and pending operations such as 4 * □; the return value flies into the box of the frame below. On the right the band of the substitution model writes down the same chain. In Fibonacci mode the call tree grows node by node; the path from the root to the current node is exactly the stack.

Try it yourself

Choose n and switch between fakultaet, fakultaetIt and fib. Counters show calls, stack depth and pending operations. A switch shows how a language with tail call optimisation would compute, explicitly not Python. With memoization, branches whose value is already in the dictionary speicher are no longer called: fib(5) needs 9 instead of 15 calls. Optionally the app first asks what a call returns. The table lists the exact numbers of calls up to fib(100).

Frequently asked questions

What is the call stack of a recursive function?
The call stack holds a frame for every function call that has not finished yet, with its names and values. When a function calls itself, a new frame is added on top; it disappears only when its return is done. For fakultaet(5) there are five frames at the deepest point.
Does tail recursion save memory in Python?
No. Python does not optimise tail calls: the tail-recursive version also creates a frame for every call and merely passes the result back. That is why fakultaetIt(1, 1, 5000) ends like fakultaet(5000) with RecursionError, since the limit is 1000 frames.
Why does recursive fib(n) need so many calls?
Every call of fib calls itself twice, and the same values are computed again and again. Without memory it takes 2·fib(n+1) − 1 calls, already 21891 for fib(20). With memoization in a dictionary every value is computed once, which takes 2n − 1 calls.
Which programs does Recursion and the Call Stack show?
Factorial, recursive and tail-recursive with fakultaetIt, tree-recursive Fibonacci and Fibonacci with memoization, each in the Python code of the lecture. Every program runs step by step with the call stack, counters and optional prediction questions, and can be copied.

Subject: Computer science

More from Computer science

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps