Variable Binding
Question 1 |
Consider the program below in a hypothetical programming language which allows global variables and a choice of static or dynamic scoping.
int i ; program main () { i = 10; call f(); } procedure f() { int i = 20; call g (); } procedure g () { print i; }Let x be the value printed under static scoping and y be the value printed under dynamic scoping. Then, x and y are
x = 10, y = 10 | |
x = 20, y = 10 | |
x = 10, y = 20 | |
x = 20, y = 20 |
Question 1 Explanation:
Since the value of x is based on static scoping, in the procedure g( ), print i will directly look into the global scope and find i=10 which was previously set by main( ) and since the value of y is based on dynamic scope, procedure g( ) will first look into the function which called it, i.e., procedure f( ) which has a local i=20, which will be taken and 20 will be printed.