Recursion visualizer
A recursive function solves a problem by calling itself on a smaller one, until it reaches a case it can answer at once. That is easy to write and hard to picture: who is waiting for whom, and where does each answer go? Pick a function, such as factorial, Fibonacci or the Tower of Hanoi, and step through it. The code marks the line that runs, the call stack shows every call that is still waiting, and the call tree grows call by call and fills in the value each one returns. Pick a function and its arguments, then press Step to follow the run one call at a time, or Run to play it. The code marks the line that runs and, lighter, the line where the caller waits. The call stack lists the calls that are still open, newest on top, with the answers they already have. The call tree grows with every call and shows the value each call returns. Back goes one step back, and the arrow keys step too.
How it works
- Pick a function, such as factorial, Fibonacci with or without a memo, the Tower of Hanoi or the one without a base case, and set its arguments.
- Press Step to follow the run one call or return at a time, or Run to play it; Back goes one step back. The status line says what just happened.
- Watch the code, the call stack and the call tree together: the stack holds the calls that are still waiting, and every return fills in a value in the tree.
The base case and the way back
Every recursive function has two parts: a base case it can answer straight away, such as fact(1) = 1, and a recursive case that hands a smaller problem to another call, such as fact(n) = n × fact(n − 1). Each call waits on the stack until the call it made returns. The stack grows on the way down to the base case and shrinks on the way back, and on the way back each waiting call finishes its own line: 2 × 1, then 3 × 2, then 4 × 6.
Why plain Fibonacci is slow, and how a memo fixes it
fib(n) calls fib(n − 1) and fib(n − 2), and both of those ask for the same smaller numbers again, so the call tree doubles almost at every level: fib(6) takes 25 calls and fib(20) takes 21,891. Turn on the memo and each answer is stored the first time it is found; a second request is answered from the memo without a single new call, and fib(n) takes only 2n − 1 calls. That is top-down dynamic programming.
When recursion never stops
A function without a base case keeps calling itself, and every call adds a frame to the stack. The stack has a limited size, so the run ends in a stack overflow: in a browser after about ten thousand frames, with the error Maximum call stack size exceeded. The countdown in the list shows it after 20 frames, with all of them still waiting for an answer that never comes.
Frequently asked questions
What is recursion?
A function that calls itself to solve a smaller version of the same problem. It needs a base case, a small input it answers directly, and every call must get closer to that base case. fact(n) = n × fact(n − 1) with fact(1) = 1 is the classic example.
What is the call stack?
The list of calls that have started but not yet returned. Each call gets a frame with its own arguments and local variables, placed on top of the stack; when it returns, its frame is removed and the call below continues from the line where it waited. The deepest the stack gets is the recursion depth.
Why does Fibonacci need so many calls without a memo?
Because fib(n − 1) and fib(n − 2) both recompute the smaller numbers. Plain recursion makes 2·F(n + 1) − 1 calls, which grows like the Fibonacci numbers themselves: 25 calls for fib(6), 21,891 for fib(20) and more than 330 million for fib(40). With a memo every value is computed once, so fib(n) takes 2n − 1 calls.
How many moves does the Tower of Hanoi need?
2ⁿ − 1 for n disks, which is the least possible: the largest disk has to move once, and before and after that the other n − 1 disks have to be moved out of the way and back. The recursive solution makes exactly those moves, with 2ⁿ⁺¹ − 1 calls including the empty ones for zero disks.
What makes the Ackermann function special?
Wilhelm Ackermann described it in 1928 as a function that is clearly computable but grows faster than any function you can build from plain loops whose length is known in advance (primitive recursive functions). ack(3, 4) = 125 already takes 10,307 calls, and ack(4, 2) has 19,729 digits.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Go to Editor