A recursive function in general is (not only in C) a function that calls itself (resorts to itself).
The first call puts the initial state then the function calls itself with the changed data (depending on the previous state), it is important to put an exit condition or else it will run ad infinitum (in practical terms until it is the memory ends).
In the case of the Fibonacci Succession
The sequence begins with the numbers 0 and 1 and from these, "each term is the sum of the previous two", is the recurrence relation that defines it.
To calculate the nth element of the Fibonacci sequence, the simplest algorithm is usually the " downstream recursive version "
in C:
int fibo(int num)
{
if (num == 0) // <- condición de salida #1
{
return 0;
}
else if (num == 1) // <- otra condición de salida #2
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2)); // <- llamada recursiva #3
}
}
example: find out the 8th Fibonacci number
fibo(8)
num vale 8
por el return fibo(8) va a ser igual a (fibo(7)+fibo(6))
fibo(7)
num vale 7
por el return fibo(7) va a ser igual a (fibo(6)+fibo(5))
fibo(6)
num vale 6
por el return fibo(6) va a ser igual a (fibo(5)+fibo(4))
fibo(5)
num vale 5
por el return fibo(5) va a ser igual a (fibo(4)+fibo(3))
fibo(4)
num vale 4
por el return fibo(4) va a ser igual a (fibo(3)+fibo(2))
fibo(3)
num vale 3
por el return fibo(3) va a ser igual a (fibo(2)+fibo(1))
fibo(2)
num vale 2
por el return fibo(2) va a ser igual a (fibo(1)+fibo(0))
fibo(1)
num vale 1
por el return (condición de salida #2) va a ser igual a 1
fibo(0)
num vale 0
por el return (condición de salida #1) va a ser igual a 0
Now we see the chain of returns
fibo(0) = 0
fibo(1) = 1
fibo(2) = 1 + 0 = 1
fibo(3) = 1 + 1 = 2
fibo(4) = 2 + 1 = 3
fibo(5) = 3 + 2 = 5
fibo(6) = 5 + 3 = 8
fibo(7) = 8 + 5 = 13
and finally return to the main with
fibo(8) = 13 + 8 = 21