Skip to main content
Recursion is a programming technique where a function calls itself to solve a problem. It works by breaking down a complex problem into smaller, more manageable sub-problems of the same type. A classic real-world analogy is a Russian nesting doll (Matryoshka): inside a big doll is a smaller doll, inside that is an even smaller doll, until you reach the smallest doll that cannot be opened.

The Two Core Rules of Recursion

Every recursive function must have two essential parts:
  1. Base Case: The stopping condition. This is the simplest possible case that can be answered directly without calling the function again. Without a base case, the function will call itself forever!
  2. Recursive Case: The part of the function where it calls itself with a modified, smaller input, moving closer to the base case.

Example 1: Countdown

Let’s look at a simple function that counts down to zero.

How it executes:

  1. countdown(3) is called. Prints 3. Calls countdown(2).
  2. countdown(2) is called. Prints 2. Calls countdown(1).
  3. countdown(1) is called. Prints 1. Calls countdown(0).
  4. countdown(0) is called. Since 0 <= 0, it matches the base case, prints "Blast off! 🚀", and returns. All function calls finish execution.

Example 2: Factorial

In mathematics, the factorial of a positive integer nn (written as n!n!) is the product of all positive integers less than or equal to nn. For example: 4!=4×3×2×1=244! = 4 \times 3 \times 2 \times 1 = 24. Mathematically, we can define it recursively:
  • 1!=11! = 1 (Base Case)
  • n!=n×(n1)!n! = n \times (n - 1)! (Recursive Case)

Visualizing the trace:


Example 3: Fibonacci Sequence

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...

Precautions & Common Pitfalls

1. Infinite Recursion (RecursionError)

If a recursive function never reaches its base case, it keeps calling itself until Python reaches its maximum call stack depth limit and crashes.
Precaution: Always ensure your recursive step modifies the input variable so that it eventually triggers the base case!

2. Performance Overhead

Each recursive call is added to the system’s “call stack” in memory. If the recursion is too deep (e.g., thousands of calls), it uses a lot of memory. For example, calculating fibonacci(40) using the simple recursive function above is extremely slow because it recalculates the same values millions of times. In such cases, an iterative loop (for or while) or caching (memoization) is preferred.

What’s next?

Now that you have completed all function concepts, test and reinforce your understanding with hands-on practice problems.

Practice & Exercises

Practice writing and debugging functions