by Pau Pavón

# How to use recursive functions to create the Fibonacci Sequence in 5 programming languages

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Recursive functions are those functions which, basically, call themselves.

All the same, for the purposes of this tutorial, let’s begin.

First of all, let’s think about what the code is going to look like. It’ll include:

· A recursive function F (F for Fibonacci): to compute the value of the next term.

· Nothing else: I warned you it was quite basic.

Let’s plan it. The code should, regardless the language, look something like this:

`function F(n)`

` if n = 0`

` return 0`

` if n = 1`

` return 1`

` else`

` return F(n-1) + F(n-2)`

But the good news is that it actually works!

### Python

`def F(n):`

` if n == 0:`

` return 0`

` if n == 1:`

` return 1`

` else:`

` return F(n-1) + F(n-2)`

### Swift

`func F(_ n: Int) -> Int {`

` if n == 0 {`

` return 0`

` }`

` if n == 1 {`

` return 1`

` }`

` else {`

` return F(n-1) + F(n-2)`

` }`

`}`

### JavaScript

`function F(n) {`

` if(n == 0) {`

` return 0;`

` }`

` if(n == 1) {`

` return 1;`

` }`

` else {`

` return F(n-1) + F(n-2);`

` }`

`}`

### Java

`public static int F(int n) {`

` if(n == 0) {`

` return 0;`

` }`

` if(n == 1) {`

` return 1;`

` }`

` else {`

` return F(n-1) + F(n-2);`

` }`

`}`

### C++

`int F(int n) {`

` if(n == 0) {`

` return 0;`

` }`

` if(n == 1) {`

` return 1;`

` }`

` else {`

` return F(n-1) + F(n-2);`

` }`

`}`