Create an animated lesson
On requestThis article is free to read. Creating a lesson uses your daily or stored credits; review the language and settings before starting.
What is recursion in programming?
ProgrammingLesson on requestFree to read
Quick answer
Recursion is when a function solves a problem by calling itself on a smaller piece of the same problem, until it reaches a simple case it can answer directly. It's a powerful way to express problems that contain smaller copies of themselves.
Key things to understand
- Every recursion needs a base case — the simplest input that stops the calls — or it runs forever.
- Each call works on a smaller version of the problem and trusts the recursion to handle the rest.
- Classic examples: factorials, the Fibonacci sequence, and traversing trees or folders.
- It often mirrors the structure of the data, making some solutions far shorter than loops.
People also ask
What is a base case?
The condition that stops the recursion — a small input the function can answer without calling itself again. Without it, recursion never ends.
Is recursion better than loops?
Neither is universally better. Recursion is cleaner for naturally nested problems; loops are often more memory-efficient. Many recursions can be rewritten as loops.
What is a stack overflow in recursion?
If recursion goes too deep (or never hits a base case), it exhausts the call stack memory and the program crashes with a stack overflow error.

