Consider the following skeletal Ada program: procedure Main is X: Integer; proce
ID: 3858415 • Letter: C
Question
Consider the following skeletal Ada program: procedure Main is X: Integer; procedure Proc1 is begin __ of Proc1 is Put (X); end; __ of Proc1 procedure Proc2 is X: Integer; begin __ of Proc 2 X:= 10; Proc1 end; __ of Proc2 begin __ of Main X:= 5; Proc2 end __ of Main Assume Put is an output statement Assume this program was compiled and executed using static scoping rules. What value of X is printed in procedure Proc1? Assume this program was compiled and executed using dynamic scoping rules. What value of X is printed in procedure Proc1?Explanation / Answer
Solution:-
A) If static scoping is used then x = 5 is printed in procedure proc1.
B) If dynamic scoping is used then x = 10 is printed in procedure proc1.
Explanation :-
Static Scoping:-
Once the global variable is assigned a value it's scope will exist throughout the program execution. Hence the answer for this is 5, as x =5 is assigned in the main procedure so it is assigned and then proc2 and proc1 called subsequently that will not change the value of x. So x = 5 is printed.
Dynamic Scoping :-
Irrespective of the value of global variable .The value assigned most recently is considered. Dynamic scoping means that x refers to the x declared in the most recent frame of the call-stack.
So in main procedure x = 5 assigned, then it called proc2, in which x = 10 is assigned. This is recent value on call stack so x = 10 is printed.