I have been assigned such a problem in my software development course. So, the normal way is to check each procedure one by one and remember each call by each subprogram, however, I am a bit lazy programmer and I have decided to take a shortcut by implementing given pseudocode in an actual programming language.
Problem statement:
procedure Main is
X, Y, Z : Integer;
procedure Sub1 is
A, Y, Z : Integer;
begin
...
end;
procedure Sub2 is
A, B, Z : Integer;
begin
...
procedure Sub4 is
A, B, W : Integer;
begin
...
end;
end;
procedure Sub3 is
A, X, W : Integer;
begin
...
end;
begin
...
end;
Consider the program above. Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during the execution of the last subprogram activated? Include with each visible variable the name of the unit where it is declared (e.x. Main.X).
- Main calls Sub1; Sub1 calls Sub3; Sub3 calls Sub2;
My Attempt:
$x = 10;
$y = 20;
$z = 30;
sub Sub2
{
return $x;
}
sub Sub1
{
local $x = 9;
local $y = 19;
local $z = 29;
return Sub2();
}
print Sub1()."\n";
I'm stuck at this point and have no idea how to change the code so that it shows me the variables. I see that solution is obvious, but I've coded in C++ and Java so far.