in Programming in C edited by
25,040 views
103 votes
103 votes

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 in C edited by
by
25.0k views

4 Comments

Val value in bar function is always 3

Am I right!
0
0
Question:- Why there is abnormal termination in bar()

It will also fill stack by calling bar(1) multiple times????
0
0

Usefull comment

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...

0
0

7 Answers

111 votes
111 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.

edited by
by

4 Comments

in such a type of recursion:

  • when do we say it goes to an infinite loop?
  • when do we say it gives memory overflow/abnormal termination?
1
1
very nice explanation
0
0

when the stack is going to full means repeatedly function is calling and stack memory is overloaded at a certain time then it will be terminated by the compiler 

when the process inside  while loop being executed multiple times and hoing to infinity then its called infinite loop.

0
0
64 votes
64 votes

ans C.

by

3 Comments

Why have you crossed return 0;

0
0
Please  someone explain could not get the bar  part
0
0
nice 2018 ,,,,m also approach same
0
0
22 votes
22 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.

4 Comments

Thanks a ton for this explanation. Finally i got it. I also find all your answers very helpful.

Thanks.
1
1
Thanks a ton, brother. Means a lot ^_^
1
1
Where does the difference come when the statement x=x+bar(val-1) is replaced by x=x+bar(--val);
0
0

 Very correct!

0
0
9 votes
9 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. 

edited by
by

4 Comments

can someone explain what actual problem is with bar?
0
0
awesome question...
0
0
reshown by
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..
0
0
Answer:

Related questions