Consider the following pseudocode x integer global procedur
Solution
A) Scope rules define the visibility rules for names in a programming language. A block defines a new scope. Variables can be declared in that scope, and aren\'t visible from the outside. However, variables outside the scope -- in enclosing scopes -- are visible unless they are overridden.
for above pseudocode :
first line of the main program is set_x(0). so set_x(n:integer) will be called here. First line of set_x(n:integer) is x=n but there is no local scope variable x in that block so it will go for global variable x and set x for 0 and next line will increment global variable x by 1. now global x=1;
second line of the main program is first(). first() called .------first line of first() is set_x(1). now here also same process after set_x(1) is called. As above it won\'t find local variable so it will set global x to 1 and increment it to 2. ------second line of first() is print_x(). After calling print_x() it won\'t find any local x to write output so it will write the global x to output. here output 2 is printed.
third line of main program is print_x(). As above it won\'t find any local x to write output so it will write the global x to output again. here output 2 is printed.
fourth line of main program is second().second() is called()------ first line of second() is declaration of local variable x.-----second line of second() is set_x(2). set_x(n:integer) is called(). As it won\'t find local variable in set_x(n:integer) it will set global x to 2 and increment it to 3.------ third line of second() is print_x(). After print_x() is called it will write local x to output. so here output is garbage value for some languages and 0 for some languages becauce we didn\'t intialize local x.
fifth line of main program is print_x(). After print_x() is called it will write global x to output. so here output is 3.
Dynamic scope rules: Using this scoping rule, we first look for a local definition of a variable. If it isn\'t found, we look up the calling stack for a definition.
for above pseudocode :
first line of the main program is set_x(0). so set_x(n:integer) will be called here. First line of set_x(n:integer) is x=n but there is no local scope variable x in that block so it will look up the calling stack (main function). In main also no local variable so it will go for global variable x and set x for 0 and next line will increment global variable x by 1. now global x=1;
second line of the main program is first(). first() called .------first line of first() is set_x(1). now here also same process after set_x(1) is called. As above it won\'t find local variable in set_x(n:integer) and calling stack first(). Now we have to check the calling procedure of first() that is main procedure. In main procedure also we don\'t find any local x so it will set global x to 1 and increment it to 2. ------second line of first() is print_x(). After calling print_x() it won\'t find any local x to write output so it will write the global x to output. here output 2 is printed.
third line of main program is print_x(). As above it won\'t find any local x in print_x() and calling stack -- main() to write output so it will write the global x to output again. here output 2 is printed.
fourth line of main program is second(). second() is called()------ first line of second() is declaration of local variable x.-----second line of second() is set_x(2). set_x(n:integer) is called(). As it won\'t find local variable in set_x(n:integer) and it go for calling stack--second(). In second() it will found local x and set x to 2 and increment it to 3.------ third line of second() is print_x(). After print_x() is called it will write local x to output. so here output is 3.
fifth line of main program is print_x(). After print_x() is called it won\'t find local x in print_x() and in main) so it will write global x to output. so here output is 2.
This is exact and detailed explanation for tracing of above pseudocode in both static and dynamic scope rules.
