FUNCTION Power(B, E) returns 1 if E = 0, otherwise it returns B * Power(B, E − 1). What does Power(3, 4) return, and how many calls are made in total?
- Winding: Power(3, 4) calls Power(3, 3), then Power(3, 2), Power(3, 1) and Power(3, 0).
- Power(3, 0) is the base case and returns 1. That is 5 calls altogether.
- Unwinding: 3 × 1 = 3, then 3 × 3 = 9, then 3 × 9 = 27, then 3 × 27 = 81.
Answer: It returns 81 after 5 calls (5 stack frames at the deepest point).