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
  • 1
    You could find out by adding print(‘fibonacci ‘ + str(n)) at the top of the function. Commented Jun 27, 2020 at 12:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.