Programming & syntax · reviewed in August 2026
Recursion
Recursion is a technique where a function calls itself to solve a problem by breaking it into smaller versions of the same problem, until it reaches a base case that resolves directly with no further calls.
def factorial(n):
if n <= 1: # base case
return 1
return n * factorial(n - 1)Frequently asked questions
What is the base case and why is it required?
It's the condition that stops the recursive calls and returns a direct value. Without a reachable base case, the function would keep calling itself indefinitely until it exhausts the call stack (a RecursionError in Python).
Is recursion always better than a loop?
No. A loop is usually more memory-efficient since it doesn't accumulate calls on the stack; recursion is preferred when the problem is naturally recursive, like walking a tree or a nested structure.
What's a case of recursion with more than one call?
When the function calls itself more than once per invocation (like naive Fibonacci); this can get very costly if repeated results aren't cached.