752 views

1 Answer

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

bar() is a recursive function which is calling itself inside a while loop. 

so at first Bar(3) will go inside while loop (3>0) and will call Bar(2) which will further call Bar(1) which will also go in while loop

While(1>0){

       x= x + bar(1-1);

}

hence it will call bar(0) and return 0 as it will no go in while loop, but after returning in Bar(1) while loop it will keep on the loop

While(1>0){

       x= x + bar(1-1);

}

as it will be true forever.

So as mentioned in the best answer of the above question:

  1.  bar(3) will call bar(2)
  2. bar(2) will call bar(1)
  3. bar(1) will call bar(0)--------------- Here bar(0) return 0 
  4. bar(1) will call bar(0)
  5. bar(1) will call bar(0)

This will be an infinite loop

Related questions

4 votes
4 votes
1 answer
3
admin asked Mar 30, 2020
2,063 views
Assume that size of an integer is $32$ bit. What is the output of following ANSI C program? #include<stdio.h struct st { int x; static int y; }; int main() { printf(%d",s...
1 votes
1 votes
1 answer
4
Arjun asked Feb 16
2,260 views
​​​​Consider the following read-write schedule $\text{S}$ over three transactions $T_{1}, T_{2}$, and $T_{3}$, where the subscripts in the schedule indicate trans...