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.