What is going on with the "return fibonacci( number-1 ) + fibonacci( number-2 )"?

Posted by user1478598 on Stack Overflow See other posts from Stack Overflow or by user1478598
Published on 2012-06-24T20:41:59Z Indexed on 2012/06/24 21:16 UTC
Read the original article Hit count: 329

Filed under:
|
|

I have problem understanding what the return fibonacci( number-1 ) + fibonacci( number-2 ) does in the following program:

import sys
def fibonacci( number ):
    if( number <= 2  ):
        return 1
    else:
        return fibonacci( number-1 ) + fibonacci( number-2 )

The problem is that I can't imagine how this line works:

return fibonacci( number-1 ) + fibonacci( number-2 )

Does the both of the "fibonacci( number-1 )" and "fibonacci( number-2 )" being processed at the same time? or the "fibonacci( number-1 )" is the first to be processed and then the second one?

I only see that processing both of them would eventually return '1' so the last result I expect to see it is a '1 + 1' = '2'

I would appreciate a lot, If someone can elaborately explain the process of its calculation.

I think this is a very newb question but I can't really get a picture of its process.

© Stack Overflow or respective owner

Related posts about python

Related posts about recursion