Recursion

Algorithms
structure

Also known as: recursive

Grade 9-12

View on concept map

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—tree traversals, fractals, file system navigation, and divide-and-conquer algorithms all rely on recursion as their natural expression.

Definition

A technique where a function calls itself to solve progressively smaller instances of the same problem. Every recursive solution has two parts: a base case that stops the recursion, and a recursive case that breaks the problem into a smaller version of itself.

💡 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—tree traversals, fractals, file system navigation, and divide-and-conquer algorithms all rely on recursion as their natural expression.

💭 Hint When Stuck

When writing a recursive function, first define the base case—the simplest version of the problem that can be answered directly. Then define the recursive case—how to reduce the current problem to a smaller version. Always verify that each recursive call moves closer to the base case.

Formal View

A recursive function f is defined as: f(n) = g(n, f(h(n))) for n > n_0 (recursive case) and f(n_0) = c (base case), where h(n) < n ensures termination.

🚧 Common Stuck Point

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

⚠️ Common Mistakes

  • Forgetting the base case, causing infinite recursion and a stack overflow error
  • Writing a recursive case that does not make the problem smaller, so the base case is never reached
  • Not trusting the recursion—trying to trace every level mentally instead of trusting that the recursive call returns the correct result for the smaller problem

Common Mistakes Guides

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. Every recursive solution has two parts: a base case that stops the recursion, and a recursive case that breaks the problem into a smaller version of itself.

When do you use Recursion?

When writing a recursive function, first define the base case—the simplest version of the problem that can be answered directly. Then define the recursive case—how to reduce the current problem to a smaller version. Always verify that each recursive call moves closer to the base case.

What do students usually get wrong about Recursion?

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

How Recursion Connects to Other Ideas

To understand recursion, you should first be comfortable with function and iteration. Once you have a solid grasp of recursion, you can move on to divide and conquer and merge sort.

💻 Animated Visualization Animated

Watch the function call itself, building a stack