19.2 Recursion

2026 Syllabus Objectives

By the end of this topic, you should be able to:

  • understand what recursion is
  • understand the essential features of recursion
  • understand how recursion is written in a programming language
  • write recursive algorithms
  • trace recursive algorithms step by step
  • understand when recursion is useful
  • show awareness of what a compiler or interpreter has to do to run recursive code
  • understand the use of stacks and unwinding

What recursion means

Recursion is a programming method where a function or procedure calls itself.

This may sound strange at first, but it is really just a way of solving a big problem by turning it into a smaller version of the same problem.

A recursive solution keeps breaking the problem into smaller parts until it reaches a point where it can stop.

A simple way to think about it is this:

  • solve a small part now
  • ask the same function to solve the smaller remaining part
  • keep doing this until the problem becomes so small that the answer is obvious

So, recursion is useful when a problem can be described in terms of smaller versions of itself.


The essential features of recursion

A correct recursive algorithm must have three important features.

1. It must call itself

A recursive function or procedure must include a statement where it calls itself again.

Without this, it is not recursion.

2. It must have a base case

A base case is the stopping condition.

This is the part where the function can give an answer directly without making another recursive call.

The base case is very important because it prevents the recursion from continuing forever.

3. It must move towards the base case

Each recursive call must make the problem smaller, simpler, or closer to the stopping condition.

If the problem does not move towards the base case, the function may never stop.

So, a good recursive algorithm must:

  • call itself
  • have a base case
  • reach the base case after a finite number of calls

If any of these are missing, the recursion will fail.


Base case and general case

Recursive algorithms are usually explained using two parts.

Base case

The base case is the simplest version of the problem.

It is the case where no more recursive calls are needed.

For example, in the factorial function:

  • (0! = 1)

This is the stopping point.

General case

The general case is the part where the function calls itself with a smaller version of the problem.

For factorial:

  • (n! = n \times (n - 1)!)

This means the function solves the problem by using the answer to a smaller factorial.

The general case must always move closer to the base case.

Sign in to view full notes