Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Consider the following skeletal C program: void fun1(void); /* prototype/ voi

ID: 3907692 • Letter: 2

Question

2. Consider the following skeletal C program: void fun1(void); /* prototype/ void fun2(void); /* prototype */ void fun3(void);/* prototype */ void main) int a, b, c void fun1(void) int b, c, d void fun2(void) t int c, d, e; void fun3(void) int d, e, f; Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last function called? Include with each visible variable the name of the function in which it was defined. a. main calls fun1; fun1 calls fun3. b. main calls fun3; fun3 calls fun2.

Explanation / Answer

a)Main calls func1;fun1 calls fun3

Ans) In dynamic scoping each function executed,new scope is pushed into stack.So here,last function call create scope of fun1() . So , we will get values of fun1() variables , that is b,c,d.

Consider the below example:-

//Prototype
void fun1(void);
void fun2(void);
void fun3(void);
//Main method
int main()
{
   int a=1, b=2, c=3;
   fun1();
   return 0;
}
//function 1 definition
void fun1(void) {
   int b=4, c=5, d=6;
   fun3();
   printf("%d %d %d ", b, c, d);
  
}
//function 2 definition
void fun2(void) {
   int c=7,d=8, e=9;
   printf("%d %d %d ",c, d,e);
}
//function 3 definition
void fun3(void) {
   int d=10,e=11,f=12;
   printf("%d %d %d ",d,e,f);
}

Output:-

10 11 12
4 5 6
Press any key to continue . . .

------------------------------------------------------------

b)Main calls func3;fun3 calls fun2

Ans) In dynamic scoping each function executed,new scope is pushed into stack.So here,last function call create scope of fun3() . So , we will get values of fun3() variables , that is d,e,f.

Consider the below example:-

//Prototype
void fun1(void);
void fun2(void);
void fun3(void);
//Main method
int main()
{
   int a=1, b=2, c=3;
   fun3();
   return 0;
}
//function 1 definition
void fun1(void) {
   int b=4, c=5, d=6;
   printf("%d %d %d ", b, c, d);
  
}
//function 2 definition
void fun2(void) {
   int c=7,d=8, e=9;
   printf("%d %d %d ",c, d,e);
}
//function 3 definition
void fun3(void) {
   int d=10,e=11,f=12;
   fun2();
   printf("%d %d %d ",d,e,f);

}

Output:-

7 8 9
10 11 12
Press any key to continue . . .