What is the point of BLOCK in Fortran?

前端 未结 3 1692
别那么骄傲
别那么骄傲 2020-12-19 05:40

I am looking at some code and there is this:

BLOCK

...lines of code...

END BLOCK

What is the purpose of BLOCK? I tried to go

3条回答
  •  感情败类
    2020-12-19 05:58

    The block construct allows you to declare entities such as variables, types, external procedures, etc which are locally known to the block but have no effect to any variables outside of the block.

    Example 1:

    IF (swapxy) THEN
      BLOCK
        REAL (KIND (x)) tmp
        tmp = x
        x = y
        y = tmp
      END BLOCK
    END IF
    

    Here the variable tmp is locally defined to help out with the swap of two variables. Outside of the block, the variable tmp is unknown or back its original form if it was defined outside of the block (see next example).

    Example 2:

    F = 254E-2
    BLOCK
      REAL F
      F = 39.37
    END BLOCK
    ! F is still equal to 254E-2.
    

    The variable F is redefined locally to the block but has no effect outside of it.

    These kinds of blocks are used to make the code more readable and easier to understand as you don't need to look to the entire subprogram to understand what the locally defined entities are. Furthermore, the compiler knows that these entities are locally defined, so it might do some more optimization.

提交回复
热议问题