So I have this problem where I have to figure out the output using two different scoping rules. I know the output using lexical scoping is a=3
and b=1
, but I am having hard time figure out the output using dynamic scoping.
Note:the code example that follows uses C syntax, but let's just treat it as pseudo-code.
int a,b;
int p() {
int a, p;
a = 0; b = 1; p = 2;
return p;
}
void print() {
printf("%d\n%d\n",a,b);
}
void q () {
int b;
a = 3; b = 4;
print();
}
main() {
a = p();
q();
}
Here is what I come up with.
Using Dynamic scoping, the nonlocal references to a
and b
can change. So I have a=2
( return from p() ), then b=4
( inside q() ).
So the output is 2 4
?
p
). – Glasswort