# Chapter 6: Challenge C

Hello! I’m having some trouble to understand how this solution of the exercise works or how the compiler execute the problem. I understand what fibonacci is and how recursive function works, but I’m not being able to see the step. Could someone explain the line in bold?

func fibonacci(_ number: Int) -> Int {
if number <= 0 {
return 0
}

if number == 1 || number == 2 {
return 1
}

return fibonacci(number - 1) + fibonacci(number - 2)
}
fibonacci(1)
fibonacci(2)
fibonacci(3)
fibonacci(4)
fibonacci(5)
fibonacci(10)

OK, well, this is the root of recursion. What it does is call another copy of the function, twice!

A recursive function like has at least two exit paths. In one a value is returned. In another, the function itself is called with a modified value, usually something a bit closer to the value that results in going to the exit path. Since you say you don’t need all this explained again, what the line in bold specifically does is to call the function again twice with values slightly closer to that exit path. On the stack it places a return of the sum of the result of both of those function calls. The map of function calls, if you were to visualise the stack, looks like a tree roots, forking and branching downward. When finally number is equal to 1 or number is equal to 2, the values are added together as function calls are are unrolled from the stack and values returned.

Thanks for your help! I could understanding where it’s going

Here I found an additional complement to this question: http://stackoverflow.com/questions/8845154/how-does-the-the-fibonacci-recursive-function-work

Please read my tutorial about recursion when you actually get a chance: https://cosminpupaza.wordpress.com/2016/01/08/iteration-vs-recursion-a-beginners-approach/ in order to understand everything even better. Let me know if you have any questions or issues regarding the whole thing afterwards.

1 Like
``````    Thanks for your help! I could understanding where it's going :slight_smile:

Here I found an additional complement to this question: http://stackoverflow.com/questions/8845154/how-does-the-the-fibonacci-recursive-function-work2``````