Program Units, Procedures, and Subprograms

A program unit is a sequence of one or more lines, organized as statements, comments, and INCLUDE directives. Specifically, a program unit can be:

An executable program is a collection of program units consisting of one main program and any number of external subprograms, modules, and block data program units.

A subprogram can be invoked by a main program or by another subprogram to perform a particular activity. When a procedure is invoked, the referenced subprogram is executed.

An external or module subprogram can contain multiple ENTRY statements. The subprogram defines a procedure for the SUBROUTINE or FUNCTION statement, as well as one procedure for each ENTRY statement.

An external procedure is defined either by an external subprogram or by a program unit in a programming language other than Fortran.

Names of main programs, external procedures, block data program units, and modules are global entities. Names of internal and module procedures are local entities.

Internal Procedures

External subprograms, module subprograms, and main programs can have internal subprograms, whether the internal subprograms are functions or subroutines, as long as the internal subprograms follow the CONTAINS statement.

An internal procedure is defined by an internal subprogram. Internal subprograms cannot appear in other internal subprograms. A module procedure is defined by a module subprogram or an entry in a module subprogram.

Internal procedures and module procedures are the same as external procedures except that:



Migration Tip:

Turn your external procedures into internal subprograms or put them into
modules. The explicit interface provides type checking.

FORTRAN 77 source

      PROGRAM MAIN
        INTEGER A
        A=58
        CALL SUB(A)     ! C must be passed
      END
      SUBROUTINE SUB(A)
        INTEGER A,B,C   ! A must be redeclared
        C=A+B
      END SUBROUTINE

Fortran 90 or Fortran 95 source

PROGRAM MAIN
  INTEGER :: A=58
  CALL SUB
  CONTAINS
  SUBROUTINE SUB
    INTEGER B,C
    C=A+B         ! A is accessible by host association
  END SUBROUTINE
END

Interface Concepts

The interface of a procedure determines the form of the procedure reference. The interface consists of:

To ascertain the characteristics of a procedure:

If a procedure is accessible in a scoping unit, it has an interface that is either explicit or implicit in that scoping unit. The rules are:

Entity Interface
Dummy procedure Explicit in a scoping unit if an interface block exists or is accessible. Implicit in all other cases.
External subprogram Explicit in a scoping unit other than its own if an interface block exists or is accessible. Implicit in all other cases.
Recursive procedure with a result clause Explicit in the subprogram's own scoping unit.
Module procedure Always explicit.
Internal procedure Always explicit.
Generic procedure Always explicit.
Intrinsic procedure Always explicit.
Statement function Always implicit.

Internal subprograms cannot appear in an interface block.

A procedure must not have more than one accessible interface in a scoping unit.

The interface of a statement function cannot be specified in an interface block.

Explicit Interface

A procedure must have an explicit interface if:

  1. A reference to the procedure appears
  2. The procedure has
  3. FORTRAN 95 BeginsThe procedure is elemental. FORTRAN 95 Ends
  4. The procedure has the FORTRAN 2003 Begins BIND FORTRAN 2003 Ends attribute.

Implicit Interface

A procedure has an implicit interface if its interface is not fully known; that is, it has no explicit interface. IBM Copyright 2003