Scope of declarations
The part of the program to which a name applies is called the scope of the declaration of that name. In most cases, the scope of the declaration of a name is determined entirely by the position where the name is declared within the program.
Implicit declarations are treated as if the name were declared in a DECLARE statement immediately following the PROCEDURE statement of the external procedure.
It is not necessary for a name to have the same meaning throughout a program. A name explicitly declared within a block has a meaning only within that block. Outside the block, the name is unknown unless the same name has also been declared in the outer block. Each declaration of the name establishes a scope and in this case, the name in the outer block refers to a different data item. This enables you to specify local definitions and, hence, to write procedures or begin-blocks without knowing all the names used in other parts of the program.
A is
actually C.A, which is 2. The output
for B is 1, as declared in procedure X.
X: proc options(main);
dcl (A,B) char(1) init('1');
call Y;
return;
Y: proc;
dcl 1 C,
3 A char(1) init('2');
put data(A,B);
return;
end Y;
end X;Thus, for nested procedures, PL/I uses the variable declared within the current block before using any variables that are declared in containing blocks.
In order to understand the scope of the declaration of a name, you must understand the terms contained in and internal to.
All of the text of a block, from the PACKAGE, PROCEDURE, or BEGIN statement through the corresponding END statement (including condition prefixes of BEGIN, PACKAGE, and PROCEDURE statements), is said to be contained in that block. However, the labels of the BEGIN or PROCEDURE statement heading the block, as well as the labels of any ENTRY statements that apply to the block, are not contained in that block. Nested blocks are contained in the block in which they appear.
Text that is contained in a block, but not contained in any other block nested within it, is said to be internal to that block. Entry names of a procedure (and labels of a BEGIN statement) are not contained in that block. Consequently, they are internal to the containing block. Entry names of an external procedure are treated as if they were external to the external procedure.
Figure 1 illustrates the scopes of data declarations.

The
brackets to the left indicate the block structure; the brackets to
the right show the scope of each declaration of a name. The scopes
of the two declarations of Q and R are
shown as Q and Q' and R and R'.
X and Y are visible
to all of the procedures contained in the package. - 1
Pis declared in the blockAand known throughoutAbecause it is not redeclared.- 2
Qis declared in blockA, and redeclared in blockB. The scope of the first declaration ofQis all ofAexceptB; the scope of the second declaration ofQis blockBonly.- 3
Ris declared in blockC, but a reference toRis also made in blockB. The reference toRin blockBresults in an implicit declaration ofRinA, the external procedure. Therefore, two separate names (RandR'in Figure 1) with different scopes exist. The scope of the explicitly declaredRis blockC; the scope of the implicitly declaredRin blockBis all ofAexcept blockC.- 4
Iis referred to in blockC. This results in an implicit declaration in the external procedureA.As a result, this declaration applies to all ofA, including the contained proceduresB,C, andD.- 5
Sis explicitly declared in procedureDand is known only withinD.
Figure 2 illustrates the scopes of entry constant and statement label declarations.

A and E.
- 1
- The scope of the declaration of the name
Ais only all of the blockA, and notE. - 2
Eis explicitly declared inAas an external entry constant. The explicit declaration ofEapplies throughout blockA. It is not linked to the explicit declaration ofEthat applies throughout blockE. The scope of the declaration of the nameEis all of blockAand all of blockE.- 3
- The label
L1appears with statements internal toAand toC. Two separate declarations are therefore established; the first applies to all of blockAexcept blockC, the second applies to blockConly. Therefore, when the GO TO statement in blockBexecutes, control transfers toL1in blockA, and blockBterminates. - 4
DandBare explicitly declared in blockAand can be referred to anywhere withinA; but because they are INTERNAL, they cannot be referred to in blockE.- 5
Cis explicitly declared inBand can be referred to from withinB, but not from outsideB.- 6
L2is declared inBand can be referred to in blockB, includingC, which is contained inB, but not from outsideB.