Recursion

Understand functions that call themselves by visualizing the call stack.

📖 What to learn on this page
✅ Must-know essentials
  • Recursion means a function that calls itself
  • Always write a base case (the stopping condition)
  • Each call is pushed onto the call stack, and the result is computed as the calls return
⭐ Read if you have time
  • Compare the shape of recursion using factorial and Fibonacci
  • Understand what causes infinite recursion and stack overflow
  • Tell which problems suit recursion and which are fine with a loop

What is recursion?

Recursion is a programming technique where a function calls itself.
Anything you can express with a loop can also be written recursively, but recursion really shines when a problem has a naturally self-similar structure.
Every recursive function needs a base case (a stopping condition). Without one, the calls continue forever and you'll hit a stack overflow.

Factorial

n! = n × (n-1) × ... × 1 can be written recursively like this:
int factorial(int n) {
    if (n <= 1) return 1;   // base case
    return n * factorial(n - 1); // recursive call
}
Execution flow for n=4:
factorial(4) → 4 × factorial(3) → 4 × 3 × factorial(2) → 4 × 3 × 2 × factorial(1) → 4 × 3 × 2 × 1 = 24

Call stack visualizer

Watch how recursive calls get pushed onto the stack and then unwound as each one returns.

Fibonacci sequence

The Fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13, ...) is a classic example of a function that makes two recursive calls.
int fib(int n) {
    if (n <= 0) return 0;
    if (n == 1) return 1;
    return fib(n - 1) + fib(n - 2);
}
Heads up: this naive implementation gets exponentially slower as n grows — even fib(40) can take several seconds. In practice, use a loop or memoization.
n =

Things to watch out for

Stack overflow
Without a base case, recursion never stops. The stack gets exhausted and your program crashes.
Performance
Recursion that repeats the same work over and over is inefficient. Consider memoization (caching results) or rewriting it as a loop.
Where recursion shines
Tree traversal, divide-and-conquer algorithms (like merge sort), and any problem that fits the pattern of mathematical induction.

Try it yourself: recursion

Practice breaking a problem into smaller problems. Write the base case first, then the recursive call.
Task 1: Sum from 1 to n
Write int sum(int n) recursively. sum(5) should return 15.
Task 2: Count characters
int len(const char *s) recursively. '\0' marks the base case: return 0 there.
Task 3: Array maximum
Write a function that returns the maximum among the first n elements. max(a, n) can be expressed using max(a, n-1).
Lessons to revisit: For function structure, revisit Lesson 26: Function basics ; for the flow of calls, revisit Lesson 27: Functions in depth .

Review Quiz

int mystery(int n) {
    if (n == 0) return 0;
    return n + mystery(n - 1);
}
printf("%d\n", mystery(5));

What is the output?

5
15
120
0
Explanation: mystery(5) = 5 + mystery(4) = 5 + 4 + 3 + 2 + 1 + 0 = 15.
This function computes the sum from 1 to n — don't mix it up with factorial (n!).

Related Lessons

Functions
Lesson 26: Functions (Basics)
Review the basics of defining functions, arguments and return values.
Functions
Lesson 27: Functions (Advanced)
Review pass-by-value, the call stack and how function calls work.
Algorithms
Lesson 45: Data Structures
Leads into trees, searching and other places where recursion shines.
Practice problems for this lessonRecursion: 12 practice problemsWrite code in your browser and get it checked automatically. Hints and solutions included →
← Previous lesson
Lesson 33: Quiz (Functions)
Next lesson →
Lesson 35: Command Line Arguments (argc/argv)
Share this article
Share on X Share on Facebook