0
def fibonacci(n):

    if n == 0 or n == 1: # base c a s e s
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))

When fibonacci(10) is called , is fibonacci(n-1) resolved first till the base case and after that,fibonacci(n-2) is resolved?

  • 1
    You could find out by adding print(‘fibonacci ‘ + str(n)) at the top of the function. –  Jun 27 '20 at 12:16

1 Answers1

1

From the docs:

Python evaluates expressions from left to right.

So yes, the evaluation order for fibonacci(n-2) + fibonacci(n-1) is as you describe: first fibonacci(n-2) is evaluated to a value, then fibonacci(n-1) is evaluated to a value, and the entire expression evaluates to the sum of these two values.

There’s nothing special about recursion in this case. Python evaluates left-to-right even if the operands aren’t recursive function calls.

Nick
  • 1,038
  • 9
  • 10