What is recursion, with a simple example?
Recursion happens when a computer function calls itself. Instead of writing a loop to repeat an action, a recursive function solves a problem by breaking it down into a smaller version of the same problem, asking itself to solve that smaller piece.
Think of it as looking up a word in a dictionary, only to find another word in the definition you do not know. You look up the new word, and if that definition also has an unknown word, you look that one up too. You keep doing this until you finally read a definition where you understand every word. Then, you can work your way back up to understand the very first word you looked up.
The Anatomy of a Recursive Function
Every valid recursive function must have two main parts: the base case and the recursive step. The recursive step is where the function actually calls itself with a slightly smaller or simpler input. This is what drives the process forward.
The base case is the stopping condition. It is a simple version of the problem that can be answered immediately without needing another function call. Without a base case, your function would keep calling itself forever.
Why Recursion Works
Recursion works because the computer uses a structure called the 'call stack' to keep track of all the active function calls. Each time the function calls itself, the computer pauses the current function and adds the new one to the top of the stack.
Once the base case is reached, that function finishes and returns its answer. The computer then goes back to the previous function, uses that answer, and finishes it. This process ripples all the way back down the stack until the very first function call gets the final answer.
Where Students Slip Up
The most common mistake when writing a recursive function is forgetting the base case, or writing a base case that the recursive step never actually reaches.
When this happens, the function calls itself endlessly. The call stack fills up with paused functions until the computer runs out of memory allocated for this task. This triggers an error you might have heard of: a 'stack overflow'.
Worked through
Write a recursive function to find the factorial of a number , written as . The factorial of an integer is the product of that number and all positive integers below it (e.g., ).
Let us define our function .
First, we need our base case. The simplest factorial is (or ). So, if , we simply return .
Next, we need our recursive step. Notice that is the same as . In general, . So, our function will return .
If we call :
- , which is not 1. We return .
- For , , not 1. We return .
- For , . This is our base case! We return .
- The previous step resolves: .
- The first step resolves: .
The final answer is .
Questions students ask
Ask about this topic
Where this comes from: Introduction to Algorithms, Third Edition (Cormen, Leiserson, Rivest, Stein) · Khan Academy: Computer Science - Recursion · OpenStax: Introduction to Python Programming
See also