The Gateway to Computer Science Excellence
+41 votes
8k views

Consider the C functions foo and bar given below:

int foo(int val) {
    int x=0;
    while(val > 0) {
        x = x + foo(val--);
    }
    return val;
}
int bar(int val) {
    int x = 0;
    while(val > 0) {
        x= x + bar(val-1);
    }
    return val;
}

Invocations of $foo(3)$ and $bar(3)$ will result in:

  1. Return of $6$ and $6$ respectively.
  2. Infinite loop and abnormal termination respectively.
  3. Abnormal termination and infinite loop respectively.
  4. Both terminating abnormally.
in Programming by Veteran (425k points)
edited by | 8k views
+18

those who didn't understand bar() in one go (like me)
bar(3)------bar(2)------bar(1)

bar(1) {                            //val = 1
    int x = 0;
    while(val > 0) {           // for this to work properly val should decr to 0
        x= x + bar(val-1);        // this call bar(0) which return 0
    }                           // hence val remain 1 and falls into infinite loop
    return val;
}
+1
thanks
+1
I concluded bar will fall in infinite loop as

condition is val>0

in bar(val -1), val is not getting modified. There are no other statements that has/modify val.

Thus, the while is definitely going in infinite loop as the variable in condition is not getting modified.

Hope this helps :)
+1
Beautiful Question Absolutely Brilliant.
0
beautiful question
+3

For those who wants to see stack function calls.

This is indeed among beautiful questions of GATE

6 Answers

+61 votes
Best answer

Answer should be C.

Consider $foo$ 

Initially $val=3$ 

$foo(val--)$

is equivalent to

  1. $foo(val)$
  2. $val=val-1$

So,

  1. $foo(3)$
  2. $val=2$

$foo(3)$ calls $foo(3)$ which in turn calls $foo(3)$ this goes on 

So, here we can see that $foo(3)$ is called infinite number of times which causes memory overflow and abrupt termination

and one more thing to observe is infinite loop is not there since the val is decremented in the first iteration. 

Consider bar.

Here, we can see the val is not decrementing in the loop

So,

  1. $bar(3)$ will call $bar(2)$
  2. $bar(2)$ will call $bar(1)$
  3. $bar(1)$ will call $bar(0)\qquad \to$ Here, $bar(0)$ $return\;0$ 
  4. $bar(1)$ will call $bar(0)$
  5. $bar(1)$ will call $bar(0)$

This will go on so here there is a problem of infinite loop but not abrupt termination since it does not cause memory overflow.

by
edited by
0
I am not able to understand the bar part could you please elaborate the statement that bar(1) will not return ?
+4
Check the while loop ... is there any statement which is reducing the value of val ?? No right ... So infinite loop ...
0
Both initially goes to infinite loop, but the first one terminates abruptly but second run still infinite. Why? it is not clear.

#include <stdio.h>

int main(void) {
    // your code goes here
    printf("%d",foo(3));
    return 0;
}

int foo(int val) {
    int x=0;
    printf("Hello");  /// Here everything looks clearly
    while(val > 0) {
        x = x + foo(val-1);
    }
    return val;
}
Why this happen, plz explain someone.
+1
I am not able to understand the bar function? Anyone help with this?
+14
val-1 decreases val by 1..so bar 3 calls bar 2..bar 2 calls bar 1..bar 1 calls bar 0..bar 0 means condition false therefore return 0..

Now, return 0 ..return statement definition is it should terminate the current function bar 0 and transfer the control to the calling function while(1>0)...

while(1>0) means infinite loop...so ans is C
+1
Yeah..Got it!!

Thank you so much
0
why bar1 will call bar0 multiples times?

in the first time only it will get back value as 0 (x=x+bar(0)=x+0=0+0=0)  from bar0.

Why is this not propagated to bar2,bar3 ???

Please explain ......
+1
Yeah you are right,now calculated value is assigned to x,after which the loop continues,i.e.,

Int bar(int val){

While(val>0)

{

x=x+bar(val-1);

/*after x=0+0; the loop is checked again,as value of val is 1 in this activation record(current function),while condition becomes true again ((val)1>0).it enters the loop again bar(val(1)-1) is called i.e., bar(0) which in turn returns 0,again x=0+0; again checks the loop,again calls ....*/

}

return val;/*this line gets executed only when the while loop becomes false.*/

}

Note: you have to re-trace to clearly understand,that return 0 happens  only when while loop becomes false,in our case only when bar(0) is called while loop becomes false.
0
by the use of stack ,,,,we can clearly, understand this problem
+1

I couldn't understand first because my sub-conscious mind treated while loop as if condition, this is kind of semi-googly question.😁

+38 votes

ans C.

by Loyal (6.8k points)
0

Why have you crossed return 0;

0
Please  someone explain could not get the bar  part
0
nice 2018 ,,,,m also approach same
+6 votes

Ans is C.
Foo(3) will give runtime error on any compiler i.e stack overflow hence abnormal termination. (You know about Post decrement)
bar(3) will not stop at all, some compilers will terminate it forcefully. Infinite Loop. 

by Boss (12.3k points)
edited by
0
Please explain what exactly is happening with bar(3)? I am unable to understand :(
0
what happens here
while(1){
 }

Infinite loop.

Again
x=5;
while(x>5){
}  

Infinite loop too.

Again
x=5;
while(x>5){
y=x-1;
}
Infinite loop too.

Unless I decrement x, it will run infinite times.   Same happens with val & bar.
0
for same program foo() , if val is predecremented, then there will be no issue. rt? means only line of postdecrement will replace with foo(--val) , then there will be no issue. rt?
+1
Yes, In foo() the problem is, it is calling itself first then decrementing, So same process is continued till stack overflow. Yes.  --val will be fine.
0
thanks
0
can someone explain what actual problem is with bar?
0
awesome question...
0
bar 3 calls bar 2..bar 2 calls bar 1..bar 1 calls bar 0...now comes the concept..

return 0 transfers the control to while(1>0) condition and we know that this condition in while loop means while loop will execute infinitely..

Please go through the previous question(GATE2017-1-35 )for clarity about the return statement..
+4 votes
In bar function,val-1 decreases val by 1..so bar 3 calls bar 2..bar 2 calls bar 1..bar 1 calls bar 0..bar 0 means condition(while 0>0) is false therefore return 0..

Now, return 0 ..return statement definition is it should terminate the current function bar 0 and transfer the control to the calling function bar 1 which is while(1>0)...

while(1>0) means infinite loop...so ans is C

Also,In foo function,val-- means post decrement operator.so val is decremented only in the next statement.so foo(val--) means foo3 calls foo3..foo3 calls foo3..foo3 calls foo3 repeatedly causing abnormal termination of the program.

Please go through the previous question(GATE2017-1-35 )for clarity about the return statement..
by Active (3.9k points)
reshown by
0
U say bar 0 means condition false which condition is false in your achknoledgement ......
0
It's the while condition. while(0>0) here.
+3 votes

May Be It Works :

by Junior (549 points)
0 votes
int foo(int val) {
    int x=0;
    while(val > 0) {
        x = x + foo(val--); 
        
/* Post decrement operator
That means foo() will be recursively called with the same value,
and after that val would be decremented.
That recursively called function will do the same in itself.
Hence, we got an infinite sequence of foo(3) --> foo(3) --> foo(3) so on.
At some point, the stack would overflow, hence abnormal termination.*/

    }
    return val;
}


int bar(int val) {
    int x = 0;
    while(val > 0) {
        x= x + bar(val-1);
        
/* bar(3) --> bar(2) --> bar(1) --> bar(0)
bar(0) would return. Back to bar(1) Val is still 1, so call bar(0)
bar(0) would return again.
Hence we got an infinite sequence of bar(1) --> bar(0) --> return to bar(1)
--> call bar(0) --> return to bar(1) --> call bar(0); so on.
Hence, ininite loop.*/

    }
    return val;
}

 

Option C

PS: Why not infinite loop in foo() ?
Because we're not technically looping around. Each foo(3) calls a new foo(3). If you make a control flow diagram, the arrow would never go backwards.

For bar() the same bar(1) keeps calling bar (0) over and over.

by Active (2.7k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,645 questions
56,586 answers
195,787 comments
101,836 users