Prec.Bas prec.BasNiv. sup.

12.4 BLOCK DATA Program Units 

The block data program unit is a special form of program unit which is required only if it is necessary to specify initial values for variables and arrays in named common blocks. The program unit starts with a BLOCK DATA statement, ends with an END statement, and contains only specification and DATA statements. Comment lines are also permitted. The block data program unit is not executable and it is not a procedure.

The next example initialises the items in the common block for the plotting package used in section 12.1, so that the initial pen position is at the origin, the scaling factor starts at one, and so on. Thus a suitable program unit would be:
        BLOCK DATA SETPLT
 *SETPLT initialises the values used in the plotting package.
        COMMON /PLOT/ OPENED, ORIGIN(2), PSCALE, NUMPEN
        LOGICAL OPENED
        INTEGER NUMPEN
        REAL PSCALE, ORIGIN
        SAVE /PLOT/
        DATA OPENED/.FALSE./, ORIGIN/2*0.0/, PSCALE/1.0/
        DATA NUMPEN/-1/
        END
A block data unit can specify initial values for any number of named common blocks (blank common cannot be initialised). Each common block must be complete but it is not necessary to specify initial values for all of the items within it. There can be more than one block data program unit, but a given common block cannot appear in more than one of them.

For compatibility with Fortran66 it is also possible to have one un-named block data program unit in a program.

Linking Block Data Program Units 

If, when linking a program, one of the modules containing a procedure is accidentally omitted the linker is almost certain to produce an error message. But, unless additional precautions are taken, this will not occur if a block data subprogram unit is omitted. The program may even appear to work without it, but is likely to produce the wrong answer.

There is a simple way to guard against this possibility: the name of the block data unit should be specified in an EXTERNAL statement in at least some of the program units in which the common block is used. There is no harm in declaring it in all of them. This ensures that a link-time reference will be generated if any of these other program units are used. There is a slight snag to this technique if an INCLUDE statement is used to bring the common block definition into each program unit including the block data unit. In order to avoid a self-reference, the include-file should not contain the EXTERNAL statement.

Despite this slight complication, this is a simple and valuable precaution. It also makes is possible to hold block data units on object libraries and retrieve them automatically when they are required, just like all other types of subprogram unit.

Prec.Bas prec.HautNiv. sup.