- What is recursion? The essential features
A routine that calls itself, with a base case to stop and a general case that converges to it.
Recursion is a technique where a routine (a function or procedure) calls itself to solve a problem. Each call works on a smaller version of the same problem until it becomes simple enough to answer directly.
Every valid recursive routine has two essential features:
| Feature | What it does |
|---|---|
| Base case | The stopping condition. It returns a value without calling the routine again, so the recursion ends. |
| General case (recursive case) | The routine calls itself, with an argument that moves toward the base case (the problem gets smaller). |
Both are needed. Without a base case (or one that can never be reached), the routine calls itself forever — this is infinite recursion, and it will crash the program (see §5).
Think of it like a set of nesting dolls: you keep opening a smaller doll (general case) until you reach the tiny solid one that cannot be opened (base case).
- Recursion = a routine that calls itself on a smaller sub-problem.
- Base case: stops the recursion, returns without recursing.
- General case: calls itself with an argument moving toward the base case.
- No reachable base case → infinite recursion → crash.