49 49 votes Consider the following C function: int f(int n) { static int r = 0; if (n <= 0) return 1; if (n > 3) { r = n; return f(n-2) + 2; } return f(n-1) + r; } What is the value of $f(5)$? $5$ $7$ $9$ $18$ Programming in C gatecse-2007 programming recursion normal + – Kathleen 17.4k views answer comment Share Follow Print See all 12 Comments 12 12 Comments reply Show 9 previous comments R2-D2 commented Jun 22, 2025 reply Follow flag I am getting 18+15 = 33, as answer. If I include last return call for f(5), can anyone tell me why we ignoring that ? 1 1 replyShare Teja25 commented Nov 9, 2025 reply Follow flag Because, we are entering if block which has return in it. 0 0 replyShare Sudo_404_Div commented Apr 4 i edited by Sudo_404_Div Apr 4 reply Follow flag VARIATION OF THE ABOVE QUESTIONLet's also solve a variation of the above question{Here, the change is that we are also modifying r when n <= 0}Note that here r has a storage class of staticThe point to note here is that the value of r in f(n-1)+r is evaluated after f(n-1) is evaluated completely.So in this case, r will be 6 for each f(n-1)+rf(0)+r=1+6=7f(1)+r=7+6=13f(2)+r=13+6=19f(3)+2=19+2=21 0 0 replyShare Please log in or register to add a comment.
Best answer 49 49 votes The answer is D. $f(5) = 18.$ $f(3) + 2 = 16 + 2 = 18$ $f(2) + 5 = 11 + 5 = 16$ $f(1) + 5 = 6 + 5 = 11$ $f(0) + 5 = 1+5 = 6$ Consider from last to first. Since it is recursive function. Gate Keeda answered Jan 22, 2015 • edited Jan 26, 2018 by kenzou Gate Keeda comment Share Follow See all 12 Comments 12 12 Comments reply Show 9 previous comments haider000 commented May 24, 2020 reply Follow flag no you can not execute two return statement in a single function as soon as the first return statement runs then all the other statement after that return statement will not execute no matter what statements they are. 3 3 replyShare Shubham Pande commented Nov 1, 2020 reply Follow flag what would be the answer if declaration of variable and its initialisation were on saperate lines?…...my doubt is regarding the fact that static only means that variable only has one copy so used by all calls so r=0 will make r 0 .In current case ...is r=0 ignored because it is combined with declaration and declaration is only done once. can somone clarify all this?? 1 1 replyShare Jain07 commented Sep 17, 2023 reply Follow flag how you have directly written f(5) 18 not understandable please explain clearly 1 1 replyShare Please log in or register to add a comment.
5 5 votes Note: Function calls are retained on the stack.\begin{align*} &\text{int } f(5) \\ &\quad \downarrow \\ &f(3) + 2 \quad \text{(18)} \\ &\quad \downarrow \\ &f(2) + 5 \quad \text{(16)} \\ &\quad \downarrow \\ &f(1) + 5 \quad \text{(11)} \\ &\quad \downarrow \\ &f(0) + 5 \quad \text{(6)} \\ &\quad \downarrow \\ &1 \quad \text{(base case)} \\ & \\ &f(5) = 18 = \text{Ans}\end{align*} Abhishek_yadav answered Nov 22, 2024 Abhishek_yadav comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Option d , solution with recursive tree Bhargobi answered Dec 28, 2024 Bhargobi comment Share Follow 0 reply Please log in or register to add a comment.