###### GATE 2019
September 2, 2023
###### Programming
September 5, 2023
###### GATE 2019
September 2, 2023
###### Programming
September 5, 2023
 Question 7

Consider the following C functions.

```  int fun1 (int n)  {                              int fun2 (int n)  {
static int i = 0;                                static int i = 0;
if (n > 0)  {                                    if (n > 0)  {
++i;                                              i = i + fun1 (n);
fun1 (n-1);                                       fun2 (n-1);
}                                                 }
return (i);                                           return (i);
}                                                 } ```

The return value of fun2 (5) is _______.

 A 55
Question 7 Explanation:
#include

int fun1(int n) {
printf(“–fun1 call–\n”);
static int i = 0;
if(n>0){
++i;
printf(“fun1(%d-1)\n”,n);
fun1(n-1);
}
printf(“fun1(%d)= %d\n”,n, i);
return(i);
}

int fun2(int n) {
printf(“\n******* fun2 call ********\n”);
static int i = 0;
if(n>0){
printf(“%d + fun1(%d)\n”, i,n);
i=i+fun1(n);
fun2(n-1);
}
printf(“fun2(%d)= %d\n”,n, i);
return(i);
}

void main()
{
printf(“final = %d\n”, fun2(5));
}

Check step by step hand run of the code to understand the recursion:
******* fun2 call ********
0 + fun1(5)
–fun1 call–
fun1(5-1)
–fun1 call–
fun1(4-1)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 5
fun1(1)= 5
fun1(2)= 5
fun1(3)= 5
fun1(4)= 5
fun1(5)= 5

******* fun2 call ********
5 + fun1(4)
–fun1 call–
fun1(4-1)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 9
fun1(1)= 9
fun1(2)= 9
fun1(3)= 9
fun1(4)= 9

******* fun2 call ********
14 + fun1(3)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 12
fun1(1)= 12
fun1(2)= 12
fun1(3)= 12

******* fun2 call ********
26 + fun1(2)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 14
fun1(1)= 14
fun1(2)= 14

******* fun2 call ********
40 + fun1(1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 15
fun1(1)= 15
******* fun2 call ********
fun2(0)= 55
fun2(1)= 55
fun2(2)= 55
fun2(3)= 55
fun2(4)= 55
fun2(5)= 55
final = 55

Question 7 Explanation:
#include

int fun1(int n) {
printf(“–fun1 call–\n”);
static int i = 0;
if(n>0){
++i;
printf(“fun1(%d-1)\n”,n);
fun1(n-1);
}
printf(“fun1(%d)= %d\n”,n, i);
return(i);
}

int fun2(int n) {
printf(“\n******* fun2 call ********\n”);
static int i = 0;
if(n>0){
printf(“%d + fun1(%d)\n”, i,n);
i=i+fun1(n);
fun2(n-1);
}
printf(“fun2(%d)= %d\n”,n, i);
return(i);
}

void main()
{
printf(“final = %d\n”, fun2(5));
}

Check step by step hand run of the code to understand the recursion:
******* fun2 call ********
0 + fun1(5)
–fun1 call–
fun1(5-1)
–fun1 call–
fun1(4-1)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 5
fun1(1)= 5
fun1(2)= 5
fun1(3)= 5
fun1(4)= 5
fun1(5)= 5

******* fun2 call ********
5 + fun1(4)
–fun1 call–
fun1(4-1)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 9
fun1(1)= 9
fun1(2)= 9
fun1(3)= 9
fun1(4)= 9

******* fun2 call ********
14 + fun1(3)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 12
fun1(1)= 12
fun1(2)= 12
fun1(3)= 12

******* fun2 call ********
26 + fun1(2)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 14
fun1(1)= 14
fun1(2)= 14

******* fun2 call ********
40 + fun1(1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 15
fun1(1)= 15
******* fun2 call ********
fun2(0)= 55
fun2(1)= 55
fun2(2)= 55
fun2(3)= 55
fun2(4)= 55
fun2(5)= 55
final = 55