To find the factorial of 5, one may reason: "5! is 5 times 4!, if only I knew 4!." The same reasoning gives 4! from 3!, and so on down to 1!. Solving a problem by referring to a smaller version of the same problem is called recursion.

What Is Recursion?

Definition: Recursion. A method of solving a problem in which a function calls itself to solve a smaller instance of the same problem.

A function that calls itself is a recursive function. Many definitions in mathematics are naturally recursive. The factorial is defined by n! = n × (n − 1)! for n greater than 0, with 0! = 1.

The Two Parts of a Recursive Solution

Every correct recursive solution has two parts.

Definition: Base case. The simplest instance of a recursive problem, which is solved directly without a further recursive call.

The recursive case reduces the problem to a smaller instance and calls the function again. Each call must move closer to the base case. If the base case is missing or can never be reached, the calls never stop, and the program fails when its memory for function calls is exhausted.

How a Recursive Call Works

When fact(3) is called, it cannot finish until fact(2) returns. That in turn waits for fact(1), which waits for fact(0). The base case returns 1, and the waiting calls then finish in reverse order.

The computer keeps the unfinished calls on a call stack. Each call has its own copy of the parameter n, so the values 3, 2, 1 and 0 exist at the same time without interfering with one another.

Recursion and Iteration

Anything that can be done by recursion can also be done by a loop. Table 4.2 compares them.

Table 4.2: Comparison of recursion and iteration

Point Recursion Iteration
Repetition by A function calling itself A loop statement
Stops when The base case is reached The loop condition becomes false
Memory Extra memory for every call No extra memory per pass
Suits Problems defined in terms of themselves Simple counting and accumulation

Recursion often gives a shorter and clearer solution when the problem itself is defined recursively. Iteration is usually faster and uses less memory.

Designing a Recursive Function

  1. Identify the base case and its answer.
  2. Express the problem in terms of a smaller instance.
  3. Check that every call moves towards the base case.

For the sum of the first n natural numbers: the base case is sum(0) = 0, and the recursive case is sum(n) = n + sum(n − 1).

Example 4.2: Recursive factorial and sum

Problem: Write recursive functions for n! and for the sum of the first n natural numbers, and test them for n = 5.

Solution:

  1. Factorial: base case 0! = 1; recursive case n × (n − 1)!.
  2. Sum: base case sum(0) = 0; recursive case n + sum(n − 1).
#include <stdio.h>

int fact(int n);
int sum(int n);

int main(void)
{
    int n = 5;

    printf("Factorial of %d = %d\n", n, fact(n));
    printf("Sum of 1 to %d  = %d\n", n, sum(n));
    return 0;
}

int fact(int n)
{
    if (n == 0)
    {
        return 1;
    }
    return n * fact(n - 1);
}

int sum(int n)
{
    if (n == 0)
    {
        return 0;
    }
    return n + sum(n - 1);
}

Output:

Factorial of 5 = 120
Sum of 1 to 5  = 15

Result: fact(5) expands to 5 × 4 × 3 × 2 × 1 × 1 = 120, and sum(5) to 5 + 4 + 3 + 2 + 1 + 0 = 15. Both functions assume the precondition n ≥ 0; a negative argument would never reach the base case.

Common mistakes

  • Omitting the base case, so that the function calls itself without end.
  • Writing a recursive call that does not move towards the base case, such as fact(n) inside fact(n).
  • Forgetting to return the result of the recursive call.

Key takeaways

  • A recursive function calls itself on a smaller instance of the problem.
  • Every recursive solution needs a base case and a recursive case.
  • Each call must move closer to the base case.
  • Recursion uses memory for every unfinished call; iteration does not.

Practice questions

  1. What are the two essential parts of a recursive function?
  2. What happens if a recursive function has no base case?
  3. Write the recursive definition of the sum of the first n natural numbers.

Answers to the practice questions

  1. The base case and the recursive case.
  2. The calls never stop, and the program fails when the memory for function calls runs out.
  3. sum(0) = 0, and sum(n) = n + sum(n − 1) for n greater than 0.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV