Multiple Recursive Calls

In this lesson, we look at recursive functions that do multiple recursive calls.

In the previous lesson, we started writing simple recursive functions. So far, all the functions that we looked at followed a simple recursive pattern of exactly one recursive call per equation. In this lesson, we will consider functions that require multiple recursive calls.

Fibonacci numbers

As an example, let’s write a function to compute the Fibonacci sequence.

The Fibonacci numbers are defined using a recursive equation. The first two Fibonacci numbers are 0 and 1. The successive numbers are obtained by adding the two previous numbers. This leads to the sequence

0,1,1,2,3,5,8,13,21,34...0, 1, 1, 2, 3, 5, 8, 13, 21, 34...

The implementation as a recursive function is straightforward:

Get hands-on with 1400+ tech skills courses.