What is the point of BLOCK in Fortran?

前端 未结 3 1694
别那么骄傲
别那么骄傲 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 06:15

    From the Fortran 2008 standard:

    The BLOCK construct is an executable construct that may contain declarations.

    It is not related to common blocks or to block data program units.

    So, the main use is this "containing declarations".

    As scoping units we have things like

    integer i
    block
      integer j ! A local integer
      integer i ! Another i
      save i    ! ... which can even be SAVEd
    end block
    

    which affords locality of declarations:

    ! ... lots of code
    block
      integer something
      read *, something
    end block
    ! ... lots more code
    

    These scoping blocks allow automatic objects:

    integer i
    i = 5
    block
      real x(i)
    end block
    

    As executable constructs, they also have useful flow control:

    this_block: block
      if (something) exit this_block
      ! ... lots of code
    end block
    

    They also have finalization control:

    type(t) x
    block
      type(t) y
    end block ! y is finalized
    end  ! x is not finalized
    

    for x and y of finalizable type.

    Oh, and let's not forget how you can confuse people with implicit typing.

提交回复
热议问题