|
|
Scope Of a Variable: Dynamic Scope
The dynamic scope of a variable
extends to all the procedures called thereafter during program execution,
until the first procedure to be called that re-declares the variable. Therefore, in order to determine the
dynamic scope of a variable, we must first know the sequence in which
procedures are called during execution of the program. Unlike static scope,
dynamic scope cannot be determined merely by studying the text of a program. Pascal uses static scope, not dynamic
scope. For purposes of discussing dynamic scope, we will assume that the
language we are considering is a Pascal-like language, and not Pascal
itself. However, using the same Pascal syntax for all the modules in the
courseware will help us focus on the concept of scope in this module. What is the scope of the variable x declared
in the procedure compute() in the following program, assuming that
procedures are called in the following order: main()
calls compute(), which in turn calls transform(), which in turn calls initialize()? program main var y: Real; procedure compute() var x : Integer;
procedure initialize() var x: Real;
begin {initialize} ... end {initialize} procedure transform() var z: Real; begin {transform} ... end {transform}
begin {compute} ... end {compute} begin {main} ... end {main} Answer: The procedure in which the variable is
declared is compute(). Therefore, compute() is in the scope of x. Procedure compute() calls the
procedure transform(). Since transform() does not re-declare x,
transform() is within the scope of x declared in compute(). Procedure transform() in turn
calls procedure initialize(). Since initialize() does re-declare
x, initialize() is not within the scope of x declared in
compute(). Program main() is not called
by/after compute(), but rather, before it. Therefore, main() is not in the scope of x
declared in compute(). Note that the concept of scope is
essential to understanding programs. In the above example, you would not be
able to correctly answer the following questions if you did not understand
the concept of scope: 1. If transform() procedure prints the value of x,
which variable would it be referencing? 2. If initialize() procedure prints the value of x,
which variable would it be referencing? 3. If main() program attempts to use the value of x,
would it be legal? Can you answer these questions? Algorithm
to determine Dynamic Scope: In order to determine the dynamic scope
of a variable in a program, it is convenient to use a directed graph of the
order in which procedures are called during the execution of the program. The
directed graph for the earlier program is shown below:
Algorithm to
determine the dynamic scope of a variable v declared in a procedure p() using
a directed graph of procedure calls: 1. Identify the sub-graph of the directed graph that begins with
the procedure p(). 2. In this sub-graph, identify the first descendant node
(i.e., procedure/function) that re-declares the variable v. Eliminate the
entire sub-graph that begins with this node. The remaining nodes in the sub-graph
identified in Step 1 are in the dynamic scope of the variable v declared
in procedure p(). Test
your understanding - Instructions: In order to better understand the concept of dynamic scope of variables, solve problems using the accompanying problet. The problet will present you with the outline of a Pascal-like program, and ask you to identify all the procedures that are in the scope of a particular variable declared in a particular procedure in the program, given a particular sequence of procedure calls during the execution of the program. Study the program and answer the problem. In order to help you read the program, the problet provides a Format menu with options such as: 1. Drawing boxes to delineate definitions of procedures in the program; 2. Changing the amount of indentation to set off different procedure definitions in the program. Follow these steps to solve each problem (See Figure below): 1. Study the Pascal-like program in the left panel. 2. Read the question posed in the top right panel. 3. Indicate your answer in the mid-right panel, and click on Check My Answer button. 4.
Study the detailed feedback provided by the problet in
the bottom right panel. If your answer is incorrect, this panel will explain
why it is incorrect. 5.
Click on Get Another Problem button to generate
another problem.
Are you ready? Click here to launch the Dynamic
Scope Problet. |