The story behind the recursion visualizer

Defining something in terms of a smaller version of itself is older than computers. Mathematicians used it to build the natural numbers, logicians to say what can be computed at all, and programmers turned it into functions that call themselves.

A call tree of fib(4) with the calls on the stack highlighted and the returned values under the finished calls
A call tree of fib(4) with the calls on the stack highlighted and the returned values under the finished calls

Numbers defined by themselves

In 1888 Richard Dedekind showed how to define addition and multiplication on the natural numbers by recursion: a + 0 = a, and a + (n + 1) = (a + n) + 1. Giuseppe Peano built his axioms of arithmetic a year later on the same idea, a first number and a successor for every number. A definition like that is not circular, because every step refers only to a smaller case, and in the end to the first one: exactly the base case of a recursive function.

What can be computed

In the 1920s and 1930s logicians made recursion the definition of computing itself. Functions built from simple ones by such step-by-step definitions are called primitive recursive. In 1928 Wilhelm Ackermann published a function that is clearly computable but grows too fast to be one of them, which showed that the class was too small. Kurt Gödel, Jacques Herbrand and Stephen Kleene then defined the general recursive functions, and in 1936 it turned out that they compute exactly what Alan Turing's machines and Alonzo Church's lambda calculus compute.

Functions that call themselves

Early programming languages such as Fortran did not allow a subroutine to call itself, because each one kept its variables in one fixed place. John McCarthy's Lisp of 1958 was built on recursion from the start. ALGOL 60 allowed recursive procedures too, which meant that every call needed its own copy of its variables. Edsger Dijkstra's 1960 paper on recursive programming described the way that is done almost everywhere today: a stack of frames, one per call that has not returned yet.

The stack and its limits

Every call puts a frame on the stack and every return takes one off, which is exactly what the visualizer shows. The stack has a fixed size, so recursion that goes too deep, or never reaches its base case, ends in a stack overflow; in a browser that happens after roughly ten thousand frames. Some languages, such as Scheme, require that a call made as the very last step of a function reuses the frame instead, so a tail-recursive function runs like a loop. And when the same smaller problems come up again and again, as in Fibonacci, remembering their answers turns recursion into dynamic programming.

Frequently asked questions

Is every recursive function also possible with a loop?

Yes. A loop with a stack of your own can do everything recursion does, because that is what the computer does underneath. For problems shaped like a tree, such as the Tower of Hanoi or permutations, the recursive version is usually much shorter and clearer.

Why is it called a base case?

It is the base the definition stands on: the case answered without recursion, such as 0 for Fibonacci or an empty word for reversing. Without one, or with calls that never reach it, the recursion goes on until the stack runs out.

What does the Tower of Hanoi have to do with recursion?

Édouard Lucas sold the puzzle in 1883, and its shortest solution is recursion in its purest form: to move n disks, move n − 1 disks out of the way, move the largest disk, and move the n − 1 disks back on top. That makes 2ⁿ − 1 moves, and the visualizer prints them one by one.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Open the recursion visualizer