In: Computer Science
procedure Example is X: Integer; procedure First is begin Put(X); -- Print X New_Line; -- Print a newline character end First; procedure Second is X: integer; procedure Third is X: Integer; begin -- Third x := 99; First; end Third; begin -- Second x := 88; Third; end Second; begin -- Example x := 77; First; Second; end Example;
a) What will be printed by this program if we assume
static scope?
b) What will be printed by this program if we assume
dynamic scope?
static scope:
In this approach the value assigned at program begin and the value remains same in entire program:
begin -- Example x := 77; // The value of x is 77 and it remains same through out the program First;
procedure First is begin Put(X); -- Print 77 New_Line; -- Print a newline character end First; Second;
procedure Second is
begin -- Second x := 88; -- The value remains same 77 Third; procedure Third is X: Integer; begin -- Third x := 99; -- The value remains same 77
First;
procedure First is begin Put(X); -- Print 77 New_Line; -- Print a newline character end First;
end Third;
end Second; end Example;
Output:
77
77
Dynamic scope:
In this approach the value changed with respect to called functions.
begin -- Example x := 77; // The value of x is 77 First;
procedure First is begin Put(X); -- Print 77 New_Line; -- Print a newline character end First; Second;
procedure Second is
begin -- Second x := 88; -- The value of x is 88 Third; procedure Third is X: Integer; begin -- Third x := 99; -- The value of x is 99
First;
procedure First is begin Put(X); -- Print 99 New_Line; -- Print a newline character end First;
end Third;
end Second; end Example;
Output:
77
99