0 0 votes in this question for foo it will be called with same value again and again which will result in stack overflow similar will be happening with bar also as it will be called with value 2 , I am unable to understand why ans is given C ,please explain in some details @Rishab Gupta_2 sir Programming in C + – I_am_winner 1.0k views answer comment Share Follow Print See all 4 Comments 4 4 Comments reply MiNiPanda commented Jul 24, 2018 reply Follow flag L1: int bar(int val) L2: { L3: int x=0 L4: while(val>0) L5: { L6: x=x+bar(val-1); L7: } L8: return val; L9: } bar(val=3) calls bar(val=2) calls bar(val=1) calls bar(val=0). Here each of the 'val' is local to their respective functions. bar(0) returns 0 to bar(1). Now x in bar(1) becomes 0+0=0. Then L7 is visited and then again L4 is visited to check the condition. What is value of val in bar(1)? It is 1 only because we didn't change it's value by pre/post decrement. So condition holds and again L5->L6->calls bar(0)-> returns to L6 to evaluate x ->L7->L5..... This loop continues b/w bar(1) and bar(0) for infinity. 0 0 replyShare I_am_winner commented Jul 24, 2018 reply Follow flag not clear what you want to convey please explain a lil more 0 0 replyShare MiNiPanda commented Jul 24, 2018 i edited by MiNiPanda Jul 24, 2018 reply Follow flag Please see the flow. Our main concern is with the transitions marked by "1", "2", "*" and "3". bar(1) calls bar(0) --> shown by transition 1 bar(0) finishes execution and pops off the stack --> show by transition 2 bar(0) returns 0 to bar(1) --> shown by transition * Now bar(1) evaluates the value of x which is 0. Where will the control go now? Since this is enclosed in a while loop so L4 should be visited again( shown by transition 3). Then the condition is checked. "val" in bar(1) is 1 and this has not been changed. So while condition is True hence again enters the loop, calls bar(0) and the same thing goes on... I hope i could give a better idea this time.. Or else see this https://gateoverflow.in/118319/gate2017-1-36 0 0 replyShare Shubham Shukla 6 commented Jul 24, 2018 reply Follow flag for bar(3) you call bar (2) ,bar(1),bar(0) at bar(0) as its while condn becomes false so you return now you return to bar(1) where your val=1 still so it satisfies while condn and enter loop again and again call bar(0) which returns again..than again bar(1) for which val=1 satisfies while(val>0) so this looping goes on infinitely between bar(1) and bar(0) Note: for each function call you have unique val variable..! 0 0 replyShare Please log in or register to add a comment.