Java and C♯
Sub-expressions are evaluated in left-to-right order. fib(n-1)
is evaluated before fib(n-2)
. See What are the rules for evaluation order in Java?
It's important to note that the order of evaluation doesn't matter here since fib()
does not have any side effects.
C and C++
The two functions are called in an indeterminate order, and once both have been called their return values are added together and returned. The left function could be called first, or the right one first, you don't know.
That may seem problematic, but it's not, because the order they're called doesn't matter. Calling fib(i)
does not have any side effects (e.g. modifying other variables, printing a message, etc.), so the two function calls are entirely independent.
One compiler might 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 one might decide to evaluate the right side before 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