Scope

 

Static Scope

 

  • Scope of a Variable

 

 

 

Dynamic Scope

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Back to the Top

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Back to the Top

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Back to the Top

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Back to the Top

 

Scope Of a Variable: Static Scope

 

 

·        Definition

·        Pragmatics

·        Testing Instructions

·        Example

·        Algorithms

·        Test Applet

 

 

Definition:

 

The static scope of a variable is the most immediately enclosing block, excluding any enclosed blocks where the variable has been re-declared.

 

In Pascal, the blocks are procedure definitions. The most immediately enclosing block is the procedure in which the variable is declared. The enclosed blocks are procedures defined inside this procedure.

 

Therefore, the static scope of a variable in Pascal is the procedure in which the variable is declared, excluding any procedures defined inside this procedure that declare another variable by the same name.

 

The static scope of a variable in a program can be determined by simply studying the text of the program. Static scope is not affected by the order in which procedures are called during the execution of the program.

 

Example:

 

What is the scope of the variable x declared in the procedure compute() in the following program?

 

program main

   var y: Real;

 

   procedure compute()

      var x : Integer;

 

      procedure initialize()

         var z: Real;

      begin {initialize}

      ...

      end {initialize}

 

      procedure transform()

         var x: 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.

 

The procedure initialize() is defined within compute(). Since initialize() does not re-declare x, initialize() is within the scope of x declared in compute().

 

The procedure transform() is defined within compute(). Since transform() does re-declare x, transform() is not within the scope of x declared in compute().

 

Program main() is also an enclosing block (procedure) of x. But, since it is not the immediately enclosing procedure, i.e., it is not the procedure in which x is declared, main() is not in the scope of x declared in compute().

 

Pragmatics:

 

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?

 

Algorithms to determine Static Scope:

 

The structure of a Pascal program, in particular, the order in which procedure definitions are nested in the program can be graphically depicted as a tree. This tree is called the static tree of the program. It is quite convenient to use the static tree of a Pascal program to determine the static scope of a variable in the program.

 

Algorithm to draw the static tree of a Pascal program:

1.  Draw the main() program as the root node of the tree.

2.  Identify all the procedures/functions directly defined in main(). Draw them as child nodes of main() node.

3.  Repeatedly carry out step 2 for all the leaf nodes of the tree, until there are no more nested procedures in the program.

In the Pascal program presented earlier, compute() is the only procedure defined directly in main(). Hence, it is the only child of main(). The procedures initialize() and transform() are defined within compute() and are its children. The static tree of the program is shown below.

 

Algorithm to determine the static scope of a variable v declared in a procedure p() using the static tree of the program:

1.  Identify the sub-tree of the static tree with procedure p() as its root.

2.  In this sub-tree, identify any descendant nodes (i.e., procedures/functions) that re-declare the variable v. Eliminate the entire sub-trees with these nodes as root.

The remaining nodes in the sub-tree identified in Step 1 are in the scope of the variable v declared in procedure p().

 

Test your understanding - Instructions:

 

In order to better understand the concept of static scope of variables, solve problems using the accompanying problet. The problet will present you with the outline of a Pascal program, and ask you to identify all the procedures that are in the scope of a particular variable declared in a particular procedure. You may solve the problems in one of two ways:

1.  You may 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:

a.  Using different colors to indicate the different levels of nesting in the program;

b.  Drawing boxes to delineate definitions of procedures in the program;

c.  Changing the amount of indentation to set off different procedure definitions in the program.

2.  Alternatively, you may study the static tree of the program and answer the problem. Choose the View menu to display the static tree of the program.

 

Follow these steps to solve each problem (See Figure below):

1.  Study either the Pascal program or its static tree 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 Static Scope Problet.