Consider the following C functions. The return value of fun2(5) is ________ .
2020
Consider the following C functions.

The return value of fun2(5) is ________ .
Attempted by 48 students.
Show answer & explanation
Correct answer: 55
Key idea: each function has its own static variable named i that retains its value across calls. The function fun1 increments its static i each time it is called with n > 0 and returns that cumulative value. The function fun2 adds the return value of fun1(n) to its own static i for each call and recurses with decreasing n.
Call fun2(5): fun1(5) increments fun1's static i from 0 to 5 and returns 5. fun2's static i becomes 0 + 5 = 5.
Call fun2(4): fun1(4) increments fun1's static i from 5 to 9 and returns 9. fun2's static i becomes 5 + 9 = 14.
Call fun2(3): fun1(3) increments fun1's static i from 9 to 12 and returns 12. fun2's static i becomes 14 + 12 = 26.
Call fun2(2): fun1(2) increments fun1's static i from 12 to 14 and returns 14. fun2's static i becomes 26 + 14 = 40.
Call fun2(1): fun1(1) increments fun1's static i from 14 to 15 and returns 15. fun2's static i becomes 40 + 15 = 55.
Therefore the return value of fun2(5) is 55.
A video solution is available for this question — log in and enroll to watch it.