Recursion function in Python

后端 未结 10 2174
庸人自扰
庸人自扰 2020-12-15 11:52

Consider this basic recursion in Python:

def fibonacci(number):
    if number == 0: return 0
    elif number == 1:
        return 1
    else:
        return          


        
10条回答
  •  醉酒成梦
    2020-12-15 12:26

    does the 'finobacci(number-1)' completes all the recursion until it reaches '1' and then it does the same with 'fibonacci(number-2)' and add them?

    Yes, that's exactly right. In other words, the following

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

    is equivalent to

    f1 = fibonacci(number-1)
    f2 = fibonacci(number-2)
    return f1 + f2
    

提交回复
热议问题