Infinite Recursion
Also known as: Unbounded recursion, Runaway recursion
Programming error in which a recursive function never reaches a base case and keeps calling itself until the program crashes or exhausts memory.
Infinite recursion happens when a function that calls itself, directly or through other functions, never reaches the condition that should stop it. Every recursive function needs a base case, an input simple enough to answer without another call, and each recursive call must move closer to that case. If the base case is missing, unreachable or wrongly written, the calls continue indefinitely. A classic example is a factorial function that checks for zero but is given a negative number, so each call subtracts one and moves further away from the stopping point.
In most languages each function call reserves space on the call stack for its arguments, local variables and return address. Because infinite recursion keeps adding frames without removing any, the stack eventually runs out of room and the program fails with a stack overflow. Many runtimes guard against this: Python, for instance, raises a RecursionError once the call depth exceeds a configurable limit that defaults to 1,000. Languages that guarantee tail-call optimization, such as Scheme, can run certain recursive functions in constant stack space, so an infinite tail recursion hangs like an infinite loop instead of crashing.
Infinite recursion is almost always a bug, and the standard defenses are simple: write the base case first, check that every path through the function makes progress toward it and validate inputs that could skip past it. Unit tests that include edge cases such as zero, empty lists and negative numbers catch many instances. A stack trace showing the same function repeated thousands of times is the usual giveaway during debugging. The idea also has a playful side in programmer culture, as in the familiar joke of a glossary entry for recursion that simply tells the reader to see recursion.
Key points
- Every recursive function needs a reachable base case, and each call must make progress toward it.
- Unchecked recursion usually ends in a stack overflow, because each call consumes a frame on the call stack.
- Python stops runaway recursion with a RecursionError once a default depth limit of 1,000 calls is exceeded.
- With tail-call optimization, some infinite recursions behave like infinite loops instead of crashing.
From the source
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.
Related entries
Recursion
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.
Debugging
Process of finding, diagnosing and fixing defects in software or hardware that cause it to behave incorrectly or unexpectedly.
Unit Testing
Practice of verifying that small, individual pieces of code, such as single functions or classes, behave correctly when tested in isolation.
Off-by-One Error
Common programming mistake in which a loop or calculation goes one step too far or stops one step short, often at array boundaries.
Memory Leak
Bug in which a program keeps holding memory it no longer needs, so its memory use grows over time and can degrade or crash the system.
Python
High-level, general-purpose programming language created by Guido van Rossum, known for readable syntax and widely used in data science and AI.
Algorithm
Finite sequence of well-defined steps for solving a problem or performing a computation, such as sorting a list or finding a route.