Based on the following code and knowing that Static scope rules apply; -List the
ID: 3590705 • Letter: B
Question
Based on the following code and knowing that Static scope rules apply;
-List the variables that each section can access as well as what program/subprogram they are in.
-List this for sub1, sub2 and sub3
Below is an example of doing this for Main:
===================================
Program: main
Variables it can access (Variable declared in)
X (Declared in Main)
Y (Declared in Main)
Z (Declared in Main)
W (Declared in Main)
===================================
Do the above for Sub1, Sub2 and Sub3 in the below program:
-------------------------------------------------------------------------------------
proc main;
var w, x, y, z;
proc sub1;
var x, z;
X := 6; Z := 7;
x :=x + y * z;
end sub1;
proc sub2;
var w, x, y;
w := 11;
subl;
end sub2;
proc sub3;
var w;
w := 1 O; x := w+x; y := x + z + 2;
end sub3;
begin
w := O; x := 1; y := 3; z := 5;
sub2; sub3;
end main.
-------------------------------------------------------------------------------------
Explanation / Answer
Program:main
X(declared in Main)
Y(declared in Main)
Z(declared in Main)
W(declared in Main)
Explanation: As they are declared in main program we can access any where in the program.
Proc sub1:
X(declared in Main)
Y(declared in Main)
Z(declared in Main)
W(declared in Main)
x (declared in sub1)
z (declared in sub1)
Explanation: Since X,Y,Z,W are declared in main and they are global variables along with these four variables x,z declared in sub1 are accessible in sub1
proc sub2:
X(declared in Main)
Y(declared in Main)
Z(declared in Main)
W(declared in sub2)
x(declared in sub2)
y(declared in sub2)
Explanation: according to static scope rules W declared in sub2 was accessible and W declared in main was not accessible along with this global variables X,Y,Z and local variables x,y are accessible in sub2.
Proc sub3:
X(declared in Main)
Y(declared in Main)
Z(declared in Main)
W(declared in sub3)
W declared in sub3 and global variables X,Y,Z are accessible in sub3.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.