28 votes 28 votes Consider the following C function: int f(int n) { static int i = 1; if(n >= 5) return n; n = n+i; i++; return f(n); } The value returned by $f(1)$ is: $5$ $6$ $7$ $8$ Programming in C gatecse-2004 programming programming-in-c recursion easy isro2008 + – Kathleen asked Sep 18, 2014 edited Jun 15, 2018 by Milicevic3306 Kathleen 14.2k views answer comment Share Follow See all 8 Comments See all 8 8 Comments reply sumit kumar commented Nov 12, 2014 reply Follow Share re: n=n+I, but how come I came here??? 0 votes 0 votes Arjun commented Nov 12, 2014 reply Follow Share Sorry, it was a typo. Corrected now. 1 votes 1 votes sumit kumar commented Nov 12, 2014 reply Follow Share when i saw the paper there was a typo error there also.now also brother return is written in a wrong way . :) 0 votes 0 votes Arjun commented Nov 12, 2014 reply Follow Share That was silly- corrected now :) 0 votes 0 votes focus _GATE commented Jan 12, 2017 reply Follow Share @Arjun sir just basic doubt plz clear here when if condition becomes true than return statement will executed so it means it will return the value of n and come out of the function call ryt ? but here wt does the meaning of last return statement ie return f(n) ?? 0 votes 0 votes BHAVESH d shah commented Jun 28, 2017 reply Follow Share might this will help to better understand 9 votes 9 votes Mohit Srivastava commented Mar 19, 2018 reply Follow Share how many times the function will return 7? one time or four times? And how it will return.Plz explain. 0 votes 0 votes Nitesh Singh 2 commented Oct 3, 2018 reply Follow Share only single time 7 will be returned. bcoz f(7) will return 7 to f(4), f(4) now will return 7 to f(2), and f(2) will return 7 to f(1) and f(1) will return 7 to main function. 0 votes 0 votes Please log in or register to add a comment.
Best answer 38 votes 38 votes Answer is $7$. As, $f(1):n=2,i=2$ $f(2):n=4,i=3$ $f(4):n=7,i=4$ $f(7):print(n) \Rightarrow 7$ <ans> sumit kumar answered Nov 12, 2014 edited Jun 15, 2018 by Milicevic3306 sumit kumar comment Share Follow See 1 comment See all 1 1 comment reply Yaswanth_1 commented Nov 10, 2023 reply Follow Share I think it is wrong because , the function calls will go like this f(1):n=2,i=2 f(2):n=4,i=3 f(4):n=7,i=4 f(7):in this case it will matches the base then return 7 f(7) function call comes out of the stack and then it will comes to the f(4) function call in the stack, at this it not matching with base case .Now n:8(4+4) and i becomes 5.now it will call the function call f(8) .f(8) matches with the base case then it returns 8. f(8) comes out the stack.In the f(4) function call last statement has returned then it will comes out of the stack. now stack pointer comes to the f(2),at this it is not matching with base .So,n=7(n=2,i=5) ,now i becomes 6.now it will f(7) function call.in the f(7) function call ,it is matching with base case it will return 7 f(7) comes out the stack.In the f(2) function call last statement has returned then it will comes out of the stack. now stack pointer comes to the f(1),at this it is not matching with base .So,n=7(n=1,i=7) ,now i becomes 6.now it will f(7) function call.in the f(7) function call ,it is matching with base case it will return 7. f(7) returns 7 and comes out of the function call .and f(1) also comes out of the stack by returning 7.In f(1) it is executing the last statement and that statement return statement .So it will comes outb of the function call. Note:In any recursion call ,function call comes out af the stack when it is executing last statement and that statement must be return statement. Function calls: 0 votes 0 votes Please log in or register to add a comment.
16 votes 16 votes Static storage class allows to initialize a variable for one time and re-initialization can't be done till end of program . Here Iteration1: n(1)<=5; and i=1; n(2)=n(1)+i(1); i(2)=i(1)+1; Iteration2: n(2)<=5; and i=2; n(4)=n(2)+i(2); i(3)=i(2)+1; Iteration3: n(4)<=5; and i=3; n(7)=n(4)+i(3); i(4)=i(3)+1; Iteration2: n(7)<=5; and program execution terminates . So 7 is returned. Palash Nandi 1 answered Nov 13, 2014 Palash Nandi 1 comment Share Follow See all 0 reply Please log in or register to add a comment.
1 votes 1 votes so how it is 7 Sheetal Kamble answered Feb 14, 2017 Sheetal Kamble comment Share Follow See all 2 Comments See all 2 2 Comments reply adithya604 commented May 6, 2017 reply Follow Share Initialize k to 1 as you have to calculate f(1). Then you will get answer as 7. 0 votes 0 votes Suneel Padala commented Dec 19, 2018 reply Follow Share @Sheetal you are not initializing k as 1 0 votes 0 votes Please log in or register to add a comment.