user1578026
user1578026

Reputation:

End statement for Fortran elements

In Fortran 77 or Fortan 90 or Fortran 2003, is it possible to end following constructs without its correspoding end statement as shown below?

For example, is it possible to end a program by just using the end statement instead of end program?

subroutine : end subroutine

function: end function

module: end module

program: end program

if: end if\endif

do: end do\enddo

select: end select\endselect

Upvotes: 1

Views: 286

Answers (2)

francescalus
francescalus

Reputation: 32366

It is possible to use an unadorned end in program units and subprograms/procedure definitions:

  • end [program]
  • end [module]
  • end [submodule]
  • end [block data]
  • end [function]
  • end [subroutine]

It is not permitted to use an unadorned end for executable constructs, interface blocks, or in "assignment constructs" or type definitions (essentially, everywhere else):

  • end associate
  • end block
  • end critical
  • end do
  • end enum
  • end forall
  • end if
  • end interface
  • end select (for each of select case, select rank and select type)
  • end team
  • end type
  • end where

Further, for the first list it is necessary to use the extended form when adding the program unit/procedure name:

  • end ... name

Note that I make no mention of language revision: some of those of the question didn't even exist in Fortran 77 and some in the lists above are new in Fortran 2018.


To briefly mention style, I have nothing to say against Ian Bush's answer.

Upvotes: 2

Ian Bush
Ian Bush

Reputation: 7433

It's a little complicated but as you include the coding style tag I would first say I would strongly recommend using the full form in all cases - any decent editor will auto-magically complete an End statement appropriately. For instance in emacs, the editor I use, hitting tab after typing End will add all the text automatically.

However it is not technically required in some cases. For control constructs such as do, if, select, where etc. the full form is required. However for the program and subprograms strictly it is not - in fact the simplest possible Fortran program is just

End

However I would recommend against this as the full form usefully documents the program at virtually zero overhead from the programmer, and it also means you don't have to remember when the full form is required, and when it is not.

I'll also add, again as you include the coding style tag, by full form I really mean the form that also includes the name of the program/subprogram, or control construct name if used. Thus I would write a "Hello World" program as

Program hello_world

  Implicit None

  Write( *, * ) 'Hello World!'

End Program hello_world

Finally I'll add nobody should be using Fortran77 in this day and age, it is quarter of a century out of date, and Fortran90 should also probably be retired for new code.

Upvotes: 3

Related Questions