BLOCK construct

Standard: F77 F90 F95 F2003 F2008 F2018 Example program


<Syntax>

block-stmt
      [ specification-part ]
      block
end-block-stmt

Syntax of the BLOCK statement

[ construct-name : ] BLOCK

If the BLOCK statement has a construct name construct-name, the END BLOCK statement shall have the same construct name.

<Semantics>

The BLOCK construct allows declarations of entities within executable code; local entities declared in the specification-part of a BLOCK construct are local to its block. A BLOCK construct accesses entities from the surrounding context by host association, so any host entity with the same name as an entity local to the BLOCK construct is inaccessible by that name. BLOCK constructs may be nested.

The specification-part of a BLOCK construct is not permitted to contain a COMMON, EQUIVALENCE, IMPLICIT, INTENT, NAMELIST, OPTIONAL or VALUE statement, and is not permitted to define a statement function. The SAVE statement is permitted in a BLOCK construct, but must have a list of entities that are to be saved.

Note that as in other host or use association contexts, the ASYNCHRONOUS and VOLATILE statements do not necessarily create a new local entity but can be used to add the attribute to an accessible host entity.

A BLOCK construct may contain a USE statement or a declaration of an external or intrinsic procedure; this allows access to “global” entities within the BLOCK without affecting the surrounding program unit.

When execution of a BLOCK construct is completed, e.g. by executing the END BLOCK construct or branching out of the block, all its local variables that do not have the SAVE attribute cease to exist. In particular, an allocated ALLOCATABLE variable will be automatically deallocated, and a variable that is finalisable will be finalised.

The BLOCK construct is particularly useful when code is included by the INCLUDE line or by macro preprocessing, as it can be used to ensure that any temporary variables needed do not interfere with entities in the including subprogram.

<Related>

END BLOCK statement