Java and C & sharp;
Subexpressions are evaluated in order from left to right. fib(n-1) evaluates to fib(n-2) . See What are the rules for evaluation order in Java?
It is important to note that the order of evaluation is not important here, since fib() has no side effects.
C and C ++
Two functions are called in an undefined order, and as soon as both are called, their return values are added together and returned. The left function could be called first or right, you do not know.
This may seem problematic, but it is not, because the order they call does not matter. The fib(i) call has no side effects (for example, changing other variables, printing a message, etc.), so the two function calls are completely independent.
One compiler may decide to evaluate the left side before the right:
1. f(3) 2. f(2) 3. f(1) 4. return 1 5. f(0) 6. return 0 7. return 1 + 0 8. f(1) 9. return 1 10. return 1 + 1
Another may decide to evaluate the right side in front of the left:
1. f(3) 2. f(1) 3. return 1 4. f(2) 5. f(0) 6. return 0 7. f(1) 8. return 1 9. return 1 + 0 10. return 1 + 1
source share