Intermediate Topic 23 of 54 Programming Fundamentals
Recursion
A function solving a problem by calling itself on smaller inputs.
How it works
A recursive function solves a problem by reducing it to smaller instances of itself. Each call gets its own stack frame, calls continue until a base case returns directly, and then results combine on the way back up. Any recursion can be rewritten with loops and an explicit stack, but recursion often mirrors the structure of the problem more naturally.
Where it is used
Factorial, Fibonacci, tree and graph traversal, divide-and-conquer algorithms such as merge sort, backtracking puzzles and parsing nested structures all use recursion. File-system walks are a familiar example.
Common pitfall
A missing or unreachable base case causes stack overflow, and naive recursion like Fibonacci repeats work exponentially; use memoization or iteration. Deep recursion on large inputs can exhaust the stack even when the logic is correct.
Where it fits in the course
Recursion is topic 23 of 54 in Programming Fundamentals, rated Intermediate. It follows Tuples. The next topic is Base Case.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Programming Fundamentals quiz.
- Explore the Recursion Tree visualizer: Fibonacci call tree with and without memoization.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Recursion?
Where do people use Recursion?
What is the most common mistake with Recursion?
What should I learn before this topic?
How can I practice it?
Is Recursion hard to learn?
How long does it take to learn Recursion?
Which topics are related to Recursion?
What should I study after Recursion?
Related topics
- Tuples (Programming Fundamentals)
- Base Case (Programming Fundamentals)
- Sets (Programming Fundamentals)
- Call Stack (Programming Fundamentals)