# BLOCK

Statement: Marks the beginning of a BLOCK construct. The BLOCK construct executes a block of statements or constructs that can contain declarations.

[name:] BLOCK

[specification-part]

block

END BLOCK [name]

name | (Optional) Is the name of the BLOCK construct.
specification-part | (Optional) Is one or more specification statements, except for the following: COMMON FUNCTION (outside of an INTERFACE block) EQUIVALENCE IMPLICIT INTENT (or its equivalent attribute) MODULE NAMELIST OPTIONAL (or its equivalent attribute) SUBROUTINE (outside of an INTERFACE block) VALUE (or its equivalent attribute) Statement functions
block | Is a sequence of zero or more statements or constructs, except for the following: CONTAINS (outside of a TYPE definition) ENTRY IMPORT (outside of a TYPE definition) Statement functions

## Description

A BLOCK construct is itself a scoping unit. Entities declared in a BLOCK construct are local to the BLOCK construct and are accessible only in that construct and in any contained constructs. A local entity in a block construct hides any entity with the same name in its host scope. No transfer of control into a block from outside the block is allowed, except for the return from a procedure call. Transfers within a block or out of the block are allowed.

If a construct name is specified at the beginning of a BLOCK statement, the same name must appear in the corresponding END BLOCK statement. The same construct name must not be used for different named constructs in the same scoping unit. If no name is specified at the beginning of a BLOCK statement, you cannot specify one following the END BLOCK statement.

You can only branch to an END BLOCK statement from within its BLOCK construct.

The SAVE attribute specifies that a local variable of a BLOCK construct retains its association status, allocation status, definition status, and value after termination of the construct unless it is a pointer and its target becomes undefined. If the BLOCK construct contains a SAVE statement, the SAVE statement cannot specify the name of a common block. A SAVE statement outside a BLOCK construct does not affect variables local to the BLOCK construct, because a SAVE statement affects variables in its scoping unit which excludes nested scoping units in it.

The statements specified within the specification-part are evaluated in a processor-dependent order, followed by execution of block. When execution exits block, all non-SAVEd automatic and allocatable local variables are deallocated.

## Example

The following shows a BLOCK construct:

    block
      integer :: i
      real :: a(n)
       do i = 1,n
         a(i) = i
       end do
      …
    end block

When control exits the bottom of the BLOCK, local variables `i` and `a` revert to their meaning outside the block.

The following example shows two nested BLOCK constructs where the inner BLOCK construct has the construct name INNER and the outer one does not have a name:

        BLOCK
          ...
          INNER: BLOCK
            ...
          END BLOCK INNER
          ...
        END BLOCK

In the following example, the appearance and the reference of the FORMAT statement are legal:

        PROGRAM MAIN
          WRITE(6, FMT=10)
          ...
          BLOCK
      10    FORMAT(“Hello”)
          END BLOCK
          ...
        END

Implicit typing is not affected by BLOCK constructs. In the following example, even if NSQP only appears in the two BLOCK constructs, the scope of NSQP is the whole subroutine S:

        SUBROUTINE S(N)
          ...
          IF (N>0) THEN
            BLOCK
              NSQP = CEILING(SQRT(DBLE(N)))
            END BLOCK
          END IF
          ...
          IF (N>0) THEN
            BLOCK
              PRINT *,NSQP
            END BLOCK
          END IF
        END SUBROUTINE S

Parent topic: A to B