Given the following pseudo-code below with nested subprograms,
(a) Under static-scoping rules, what will be printed out?
(b) Under dynamic-scoping rules, what will be printed out?
(c) Name a disadvantage of dynamic scoping.
var x, y;
function sub2() {
var y;
y = 10;
document.write("x = " + x + "" + “y = “ + y + “ “);
}
function sub1() {
var x;
x = 30;
document.write("x = " + x + "" + “y = “ + y + “ “);
sub2();
document.write("x = " + x + "" + “y = “ + y + “ “);
}
x = 20;
y = 50;
sub1();
Static Scoping: Look for the definition within a block then enclosing block then global variables.
Dynamic Scoping: Look for the definition within a block then enclosing block then calling Function then global variables.
Get Answers For Free
Most questions answered within 1 hours.