Skip to main content
Basic Programming In C
Basic Programming In C
2h

Recursion

Another short chapter on a key concept in algorithms: recursion. A function or procedure is said to be recursive if its code calls itself. This type of subprogram allows implementing complex algorithms without using loops. The main difficulty with such algorithms is ensuring the termination of the program.

Definition and Use

:::warning Recursion An algorithm (function or procedure) is said to be recursive if its definition (its code) contains a call to itself. A recursive algorithm is the opposite of an iterative algorithm. :::

Recursive algorithms are used in different contexts:

  • computing recursive sequences (numerical for example)
  • strategies based on the divide and conquer principle, such as search or sorting algorithms
  • computation on inductive data structures (lists, trees…), see the Advanced Programming course (S6 IMA3).

:::info Note Even if certain contexts lend themselves more naturally to the use of recursive algorithms, it is always possible to write an iterative version of the algorithm. :::

Classic Examples

The most relevant example to illustrate recursive algorithms is the computation of n!. Indeed, we rely on the recursive formulation of the sequence where n! = n × (n-1)! and 1! = 0! = 1. The algorithm is as follows:


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

When writing a recursive algorithm, you need to be careful about the definition of the function (in particular ensuring it terminates) and its use (in particular its call and return type).

Another classic example is the Fibonacci sequence defined as follows: u_n = u_(n-1) + u_(n-2) and u_0 = 0, u_1 = 1.


int fibo(int n)
{
    if ( 0 == n )       return 0;
    else if ( 1 == n )  return 1;
    else                return fibo(n-1) + fibo(n-2);
}

Finally another classic example:


int sum(int n, int r)
{
    if ( 1 == n )   return r+1;
    else            return (n-1, r+1);
}

What does this algorithm do?

:::info Note r is called an accumulator parameter. :::

Tail Recursion

:::warning Tail Recursion A recursive algorithm is said to be tail recursive if the recursive call is the last instruction performed. :::

As a result, a tail-recursive algorithm does not need to store the value obtained by recursion. For example:

  • fact(n) is not tail recursive because the result of fact(n-1) is stored to be multiplied by n
  • sum(n,r) is tail recursive since there is no storage of a result i.e. sum(n,r) = sum(n-1, r+1) = sum(n-2, r+2) = ...

Transforming Recursive to Iterative

The transformation from recursive to iterative depends on the algorithm but generally involves a loop. For example, the computation of factorial, recursive version,


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

becomes in iterative form


int fact_iter(int n)
{
    int f = 1;
    for (int i = 0; i < n; i++)
    {
        f *= i;
    }
    return f;
}