ALGOL programs can include the following types of blocks: the outer block, PROCEDURE declarations, and simple blocks (BEGIN...END groups that include at least one declaration). The scope of most types of objects in ALGOL is subject to the scope limits previously described for WFL. That is, the scope of an object is limited to the following blocks:
-
The block in which the object is declared
-
Any blocks that are nested in the declaration block and that occur after the declaration of the object
However, ALGOL provides special constructs that can be used to increase the scope of procedure, interrupt, and switch label declarations. These constructs, which are called forward reference declarations, allow two objects to contain references to each other, without violating the rule that objects must be declared before they can be used. The following example illustrates this and other aspects of ALGOL scope rules.
100 BEGIN 110 INTEGER OUTER_INT1; 120 130 PROCEDURE SECONDPROC; % Forward reference declaration 140 FORWARD; 150 160 PROCEDURE FIRSTPROC; 170 BEGIN 180 INTEGER FIRST_INT; 190 OUTER_INT1:= * + 1; 200 FIRST_INT:= * + 1; 210 % Begin simple block 220 BEGIN 230 INTEGER SIMPLE_INT; 240 SIMPLE_INT:= OUTER_INT1 + FIRST_INT; 250 END; 260 % End simple block 270 IF OUTER_INT1 + FIRST_INT < 100 THEN 280 SECONDPROC; 290 END FIRSTPROC; 300 310 PROCEDURE SECONDPROC; 320 BEGIN 330 FIRSTPROC; 340 END; 350 360 INTEGER OUTER_INT2; 370 % Beginning of outer block statements 380 OUTER_INT1:= 1; 390 OUTER_INT2:= 1; 400 SECONDPROC; 410 END.
Two integers are declared in the outer block of this example: OUTER_INT1 and OUTER_INT2. Of these, OUTER_INT1 is visible to the outer block, FIRSTPROC, SECONDPROC, and the simple block inside FIRSTPROC. However, OUTER_INT2 is visible only to the outer block, because it follows all the nested procedure declarations.
Procedure FIRSTPROC is visible to SECONDPROC, because both are declared in the outer block, and the declaration of FIRSTPROC precedes that of SECONDPROC. Procedure SECONDPROC is visible to FIRSTPROC because of the forward reference declaration that precedes FIRSTPROC.
Integer SIMPLE_INT is visible only in the simple block in which it is declared, because there are no other blocks nested within the simple block.
The next example shows the use of global objects in ALGOL to provide an elementary type of IPC.
100 BEGIN 110 EBCDIC ARRAY MSG[0:71]; 120 EVENT E; 130 TASK S1, S2; 140 PROCEDURE SUBONE; 150 BEGIN 160 REPLACE MSG BY “ENTER A MESSAGE PLEASE”; 170 ACCEPT(MSG); 180 CAUSE(E); 190 END SUBONE; 200 PROCEDURE SUBTWO; 210 BEGIN 220 WAIT(E); 230 DISPLAY(MSG); 240 END SUBTWO; 250 260 PROCESS SUBONE [S1]; 270 PROCESS SUBTWO [S2]; 280 290 WHILE S1.STATUS GTR VALUE(TERMINATED) OR 300 S2.STATUS GTR VALUE(TERMINATED) DO 310 WAITANDRESET(MYSELF.EXCEPTIONEVENT); 320 END.
In this example, the two procedures SUBONE and SUBTWO are able to communicate through the EBCDIC array MSG, which is declared globally to the two procedures. This example is similar to the example shown earlier at the end of the Communication through Global Objects in WFL subsection. The major difference is that this ALGOL example regulates the timing of the tasks with a special type of object called an event. Thus, the WAIT(E) statement at line 220 causes SUBTWO to wait until SUBONE executes the CAUSE(E) statement at line 180. For further information about events, refer to Using Events and Interlocks.

