Recursion

Also known as: recursive

structure

A technique where a function calls itself to solve progressively smaller instances of the same problem. Recursion produces elegant, concise solutions for problems with naturally self-similar structure.

💡 Intuition

Russian dolls—open one, find a smaller one inside. Repeat until you reach the smallest.

Core Idea

Every recursion needs a base case (when to stop) and a recursive case (how to continue).

🔬 Example

Factorial: 5! = 5 \times 4!, \quad 4! = 4 \times 3!, \quad \ldots \quad 1! = 1 \text{ (base case)}

🎯 Why It Matters

Recursion produces elegant, concise solutions for problems with naturally self-similar structure.

⚠️ Common Confusion

Without a base case, recursion never ends—the program crashes with a stack overflow error.

Related Concepts

Prerequisites

How Recursion Connects to Other Ideas

To understand recursion, you should first be comfortable with function and iteration.

Learn More

Common Mistakes Guides

Go Deeper

Frequently Asked Questions

What is Recursion in CS Thinking?

A technique where a function calls itself to solve progressively smaller instances of the same problem.

Why is Recursion important?

Recursion produces elegant, concise solutions for problems with naturally self-similar structure.

What do students usually get wrong about Recursion?

Without a base case, recursion never ends—the program crashes with a stack overflow error.

What should I learn before Recursion?

Before studying Recursion, you should understand: function, iteration.

💻 Animated Visualization Animated

Watch the function call itself, building a stack