Separate module procedures

Standard: F77 F90 F95 F2003 F2008 F2018 Example program


<Syntax>

A “separate module procedure” is a procedure whose interface is declared in the specification part of a module, but whose definition may provided either in the module itself, or in a submodule of that module. The interface of a separate module procedure is declared by using the MODULE keyword in the prefix of the FUNCTION or SUBROUTINE statement.

A separate module procedure may be defined by a module subprogram that also uses the MODULE keyword in its FUNCTION or SUBROUTINE statement. This definition must have exactly the same characteristics, the same names for the dummy arguments, the same name for the result variable (if a function), and if it uses BIND(C), the same binding-name. Also, it must be RECURSIVE if and only if the interface is declared so. (The compiler will check that all these are correct.) In Fortran 2018, procedures are RECURSIVE by default, and so this requirement becomes that it must be NON_RECURSIVE if and only if the interface is so declared.

Alternatively, a separate module procedure may be defined by a separate module subprogram, which has the syntax:
      mp-subprogram-stmt
            specification-part
            execution-part
            internal-subprogram-part
      end-mp-subprogram-stmt
where
      mp-subprogram-stmt ::= MODULE PROCEDURE procedure-name
and
      end-mp-subprogram-stmt ::= END PROCEDURE [ procedure-name ]

The separate module subprogram syntax automatically provides the characteristics, dummy argument names, etc..

Note that although the mp-subprogram-stmt has similar syntax to the MODULE PROCEDURE statement, it appears in a different place and has different semantics.

<Semantics>

Separate module procedures are called “separate” because they allow the separation of the interface (declared in the module specification part) and the definition. If submodules are being used, the definition need not be in the same file as the interface declaration.

An important aspect of the interface for a separate module procedure is that, unlike any other interface body, it accesses the module by host association without the need for an IMPORT statement. This makes it easier to use derived types and named constants defined by the module.

<Related>

FUNCTION statement, INTERFACE statement, INTERFACE ASSIGNMENT statement, INTERFACE OPERATOR statement, MODULE statement, SUBMODULE statement, SUBROUTINE statement