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

Consider the following skeletal program: procedure Main is X: Integer; procedure

ID: 3858249 • Letter: C

Question

Consider the following skeletal 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

In Static scope, a variable's definition is resolved by searching its containing block or function, then if that fails searching the outer containing block, and so on.

Solution a) X = 5

In Dynamic scope, the calling function is searched, then the function which called that calling function, and so on, progressing up the call stack.

Solution b) X = 10