Factorial
n! = n × (n − 1)!, with 1! = 1 as the base case. The classic first example.
Free Tool
Recursion is when a function solves a problem by calling itself on a smaller version of the same problem until it reaches a base case it can answer directly. Each call waits on the call stack until the one above it returns.
Step through the examples below one call and return at a time, break the base case to see a stack overflow, then fix it. Everything runs in your browser.
Interactive visualizer
Each step is one call or one return. Watch frames pile up until the base case answers, then unwind as each caller uses the value below it.
Code
Call stack (1 frame, top first)
Step 1 of 8 · Call
Call factorial(4). A new frame with n = 4 goes on top of the stack.
Anatomy
n <= 1.n * factorial(n - 1).n < 0 does, the answer is wrong or the calls never stop.Call stack
Each call pushes a frame holding its own copy of the arguments and local variables. The newest frame runs; the ones below it wait. When the base case returns, frames pop off one by one and each caller finishes its work with the value it got back. Runtimes cap how deep the stack can grow, so endless recursion ends in a stack overflow such as JavaScript's RangeError: Maximum call stack size exceeded.
Examples
n! = n × (n − 1)!, with 1! = 1 as the base case. The classic first example.
fib(n) = fib(n − 1) + fib(n − 2). Two calls per step, so it shows why memoization matters.
Visit a node, then recurse into each child. The DOM, JSON and file systems are all trees.
Flatten or sum lists inside lists of any depth, like the second example above.
Merge sort and quicksort split the input in half, sort each half recursively and combine.
Sudoku solvers and maze search try a choice, recurse, and undo it if it fails.
Compare
| Aspect | Recursion | Loop |
|---|---|---|
| Reads best for | Trees, nested data, divide and conquer | Counting, simple sequences, streaming |
| Memory | One stack frame per active call | Constant extra memory |
| Stopping rule | Base case | Loop condition |
| Common bug | Missing or unreachable base case (stack overflow) | Off-by-one loop bounds (infinite loop) |
| Rewrite | Any recursion can become a loop with an explicit stack | Any loop can become tail recursion |
How to
Step 1
Decide which input you can answer directly, such as n <= 1 or an empty list. That is the base case.
Step 2
Write the recursive case so each call works on a strictly smaller input, such as n - 1 or one nested list.
Step 3
Use the value the smaller call returns to build this call's answer, such as n * factorial(n - 1).
Step 4
Step through a tiny input by hand or in the visualizer and check that every path reaches the base case.
Code Card turns your Claude Code, Codex, and OpenClaw sessions into a public developer profile with activity graphs, token breakdowns, badges, and a shareable card. Start with npx code-card.
FAQ
Recursion is when a function solves a problem by calling itself on a smaller piece of the same problem. Every recursive function needs a base case that returns an answer directly and a recursive case that moves toward that base case.
The base case is the input small enough to answer without another call, such as n <= 1 for factorial or a plain number in a nested list. Without a reachable base case the calls never stop and the program runs out of stack space.
The call stack is the list of function calls that have started but not finished. Each call pushes a frame holding its own arguments and local variables, and each return pops that frame and hands its value to the caller below it.
A stack overflow happens when recursion goes deeper than the runtime allows, usually because the base case is missing, never reached, or the input is very large. In JavaScript the error is RangeError: Maximum call stack size exceeded.
Neither is always better. Recursion reads naturally for trees, nested data and divide-and-conquer algorithms. A loop uses constant stack space and is often faster for simple counting. Any recursive function can be rewritten with a loop and an explicit stack.