Programming concepts
What is Recursion?
Definition
Recursion is when a function solves a problem by calling itself on a smaller version of that problem until it reaches a base case it can answer directly.
Also known as: Recursive function
Recursion in more detail
Every correct recursive function has two parts. The base case answers the smallest input outright, and the recursive case reduces the problem and calls itself. Getting either wrong is fatal in a specific way: no base case, or a recursive call that fails to make the input smaller, means the function never stops and the call stack overflows.
It suits data that is itself recursive — trees, nested directories, JSON documents, syntax trees, linked lists — where an iterative version has to maintain an explicit stack and ends up being the same algorithm with more bookkeeping. Traversing a file tree or walking a DOM is far clearer written recursively.
The cost is the call stack. Each pending call holds a frame, so depth is bounded by memory; Python defaults to a limit near a thousand frames and raises RecursionError, while JavaScript engines throw a stack overflow after a few thousand to tens of thousands. Some languages eliminate tail calls, reusing the frame when the recursive call is the last thing the function does — but note that CPython does not, and although tail call optimisation is in the ECMAScript specification, only Safari's JavaScriptCore actually implements it. Deep recursion in Python or Node should be rewritten as a loop.
Naive recursion can also repeat work exponentially. The textbook Fibonacci recomputes the same subproblems over and over, which memoisation — caching each result the first time it is computed — collapses back to linear. That pairing of recursion with a cache is the essence of dynamic programming.
Recursion example
def factorial(n):
if n <= 1: # base case: stop here
return 1
return n * factorial(n - 1) # smaller input each time
# The stack, unwound
# factorial(4) -> 4 * factorial(3)
# 3 * factorial(2)
# 2 * factorial(1) -> 1
# = 24
Natural fit: recursive data
def walk(node):
yield node
for child in node.children:
yield from walk(child)
Exponential without a cache
fib(35) naive -> ~30,000,000 calls
with memoisation -> 35 calls
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)Recursion — common questions
- What is a base case?
- The condition under which the function returns without calling itself again. It is what makes recursion terminate; without one, or without progress toward one, the calls continue until the stack overflows.
- Is recursion slower than a loop?
- Usually slightly, because each call allocates a stack frame, and it is bounded by stack depth where a loop is not. The gap rarely matters compared with clarity — but genuinely deep recursion in Python or JavaScript should be rewritten iteratively.
- What causes a stack overflow?
- Recursion that never reaches its base case, or legitimate recursion deeper than the stack allows. Check that every recursive call moves toward the base case, and convert to an explicit loop or queue when depth depends on unbounded input.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary