Recursion
Also known as: Recursive function
Technique in which a function solves a problem by calling itself on smaller instances of the same problem until it reaches a simple base case.
Recursion is a way of defining something in terms of itself. In programming, a recursive function calls itself with a smaller or simpler version of its input and relies on a base case that can be answered directly. Computing the factorial of 5 illustrates the pattern: factorial(5) is 5 times factorial(4), which is 4 times factorial(3), and so on until factorial(1), the base case, returns 1 and the pending multiplications complete. Recursion appears throughout mathematics and linguistics as well as computing.
Many problems have a naturally recursive structure. Walking a folder hierarchy, parsing nested expressions and processing tree structures all involve parts that look like smaller copies of the whole. Divide-and-conquer algorithms such as merge sort split a problem in half, solve each half recursively and combine the results. Lisp, created by John McCarthy in the late 1950s, made recursion a central programming technique, and many functional languages use it in place of loops. Under the hood, each recursive call occupies a frame on the call stack until it returns.
Recursion can produce short, elegant code, but it has costs. Deep recursion consumes stack memory, and a missing or unreachable base case leads to infinite recursion. Naive recursive solutions can also repeat work: computing Fibonacci numbers by calling the function twice per step recalculates the same values an exponential number of times, a problem solved by memoization or dynamic programming. Some compilers apply tail-call optimization, turning certain recursive calls into loops. Any recursive algorithm can be rewritten iteratively with an explicit stack, and programmers choose whichever is clearer.
Key points
- A recursive function needs a base case that stops the recursion and a recursive case that moves toward it.
- Problems with self-similar structure, such as trees and nested expressions, are often easiest to express recursively.
- Each recursive call uses stack space, so very deep recursion can overflow the call stack.
- Memoization and dynamic programming prevent naive recursion from repeatedly recomputing the same subproblems.
Related entries
Infinite Recursion
Programming error in which a recursive function never reaches a base case and keeps calling itself until the program crashes or exhausts memory.
Memoization
Optimization technique that caches a function's results for given inputs so that repeated calls with the same arguments return instantly.
Dynamic Programming
Algorithmic technique that solves complex problems by breaking them into overlapping subproblems and storing each result so it is computed only once.
Tree (Data Structure)
Hierarchical data structure of nodes connected by edges, with a single root and no cycles, used for file systems, indexes and parsers.
Functional Programming
Programming paradigm that builds software by composing pure functions, treating computation as evaluating expressions rather than changing state.
Lisp
Family of programming languages originated by John McCarthy in 1958, built on symbolic expressions and long associated with AI research.
Algorithm
Finite sequence of well-defined steps for solving a problem or performing a computation, such as sorting a list or finding a route.
Higher-Order Function
Function that takes other functions as arguments or returns a function as its result, such as map, filter and reduce.
Referenced by
Other entries that discuss or point to Recursion.