Scope of an Identifier

The scope of an identifier is the portion of an ALGOL program in which the identifier can successfully be used to denote its corresponding values and characteristics.

In one part of an ALGOL program, an identifier can be used to denote one set of values and characteristics, while in another part of the program; the same identifier can be used to denote a different set of values and characteristics.

For example, in one block the identifier EXAMPLE_IDENT can be declared as a REAL variable. That is, the identifier can be used to store single-precision, floating-point arithmetic values. Such an identifier could be assigned the value 3.14159. In another block of the same program, EXAMPLE_IDENT can be declared as a STRING variable. In this block, EXAMPLE_IDENT could be assigned the value ALGOL IS A HIGH-LEVEL, BLOCK-STRUCTURED LANGUAGE.

Although EXAMPLE_IDENT can be of type real and of type string in the same program, within a specific block EXAMPLE_IDENT has only one type associated with it. In general, the scope of an identifier is always such that within a given block, the identifier has associated with it at most one set of values and characteristics.

The scope of an identifier is described by rules that define which parts of the program are included by the scope, which parts of the program are excluded by the scope, and the requirements for uniqueness placed on the choice of identifiers. These general rules are described as follows.

Local Identifiers

An identifier that is declared within a block is referred to as local to that block. The value or values associated with that identifier inside the block are not associated with that identifier outside the block. In other words, on entry to a block, the values of local identifiers are undefined; on exit from the block, the values of local identifiers are lost. An identifier that is local to a block is global to blocks occurring within the block. When a block is exited, identifiers that are global to that block do not lose the values associated with them. The properties of global identifiers are described more completely below.

Global Identifiers

An identifier that appears within a block and that is not declared within the block, but is declared in an outer block, is referred to as global to that block. A global identifier retains its values and characteristics as the blocks to which it is global are entered and exited.

As the following program illustrates, an identifier can be local to one block but global to another block:

BEGIN
   FILE PRTR(KIND = PRINTER);
   REAL A;
   A := 4.2 @ -1; % FIRST STATEMENT OF OUTER BLOCK
   BEGIN
      LIST L1 (A);
      INTEGER A;
      LIST L2 (A);
      A := 3; % FIRST STATEMENT OF INNER BLOCK
      WRITE (PRTR, */, L1);
      WRITE (PRTR, */, L2);
   END; % OF INNER BLOCK
   A := A*A;
   WRITE (PRTR, */, A);
END. % OF PROGRAM

In the preceding example, the identifier A that is declared REAL is global to the inner block. The A declared as type INTEGER in the inner block is local to the inner block, so when the inner block is exited, the integer A and its value, 3, are lost. Within the scope of integer A, a reference to A is a reference to the integer A, not to the global, real A. At the time the declaration for list L1 is compiled, the declaration for local A has not been seen, so list L1 contains the global, real A. However, the list L2 contains the local, integer A. The A referenced in the outer block is the A that was declared REAL and assigned the value 4.2 @ -1. The result of the first WRITE statement is A=0.42. The result of the second WRITE statement is A=3. The result of the third WRITE statement is A=0.1764, which equals 4.2 @ -1 * 4.2 @ -1.

Global identifiers are used in inner blocks for the following reasons:

  • To carry values that have been calculated in an outer block into the inner block

  • To carry a value calculated inside the block to an outer block

  • To preserve a value calculated within a block for use in a later entry to the same block

To transmit a value from one block to another block that does not contain and is not contained by the first block.