... is true, line 23 returns number
. Interestingly, if number
is greater than 1, the recursion step (line 26) generates two recursive calls, each for a slightly smaller problem than the original call to fibonacci
.
Evaluating fibonacci(3)
Figure 6.27 shows how function fibonacci
would evaluate fibonacci(3)
. This figure raises some interesting issues about the order in which C++ compilers evaluate the operands of operators. This is a separate issue from the order in which operators are applied to their operands, namely, the order dictated by the rules of operator precedence and associativity. Figure 6.27 shows that evaluating fibonacci(3)
causes two recursive calls, namely, fibonacci(2)
and fibonacci(1)
. In what order are these calls made?
Get C++ How to Program, 10/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.