26 votes 26 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 asked Sep 21, 2014 • edited Oct 17, 2018 by Mk Utkarsh Kathleen 10.4k views answer comment Share Follow See all 8 Comments See all 8 8 Comments reply Show 5 previous comments blackflamemaster commented Dec 25, 2022 reply Follow Share Yes, can you please explain this? How it becomes unreachable? 1 votes 1 votes mahendrapatel commented Jan 15, 2023 reply Follow Share How static variable is assigned by local variable n??? It should error 0 votes 0 votes parag parab commented Jan 2 reply Follow Share https://www.youtube.com/watch?v=JjQdpl4kmxE 0 votes 0 votes Please log in or register to add a comment.
Best answer 36 votes 36 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 See all 12 12 Comments reply Aditya_kavathekar commented May 9, 2017 reply Follow Share I don't understand one thing Each time f() gets called 'r' will be initialised to zero So we should add 0 not 5 2 votes 2 votes SiddharthMahapatra commented Jul 22, 2017 reply Follow Share No, it's correct to add 5. Reason : static keyword is used to declare & initialize ' r '. Since static variables are declared & initialized only ONCE; so despite of several function calls, the static variable will retain it's previous value/scope. 9 votes 9 votes eyeamgj commented Sep 10, 2017 reply Follow Share before statement returnf(n-1)+r; there should be else ?? or if not then how to conclude that f(5) will not execute this line 0 votes 0 votes Arjun commented Sep 10, 2017 reply Follow Share All statements after a return statement are not executed. So, if a return statement is inside "if", there is no need to give "else" following it. 18 votes 18 votes Ravit Anand commented Dec 11, 2017 reply Follow Share After returning of f(3) it will come out of the if statement and again execute f(n-1)+r becoz its not in else part. Correct me if i am wrong 0 votes 0 votes Aspirant commented Jan 5, 2018 reply Follow Share After calculating 18 , can't we again start doing f(4) +5 ? Plz anyone explain 0 votes 0 votes Manoja Rajalakshmi A commented Jan 23, 2018 reply Follow Share I think it is not in a while loop to execute for n=4,3 etc.. 0 votes 0 votes Nitesh Singh 2 commented Jan 14, 2019 reply Follow Share how do you know value of static variable r will not change through out the program. 0 votes 0 votes haider000 commented May 24, 2020 reply Follow Share it is the property of static variable it nitiliased only once , so after every new call it will maintain their last value you should refer to the standard c book by dennis ritchie you will find there 0 votes 0 votes haider000 commented May 24, 2020 reply Follow Share 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. 1 votes 1 votes Shubham Pande commented Nov 1, 2020 reply Follow Share 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?? 0 votes 0 votes Jain07 commented Sep 17, 2023 reply Follow Share how you have directly written f(5) 18 not understandable please explain clearly 0 votes 0 votes Please log in or register to add a comment.