Modules

A module contains specifications and definitions that can be accessed from other program units. These definitions include data object definitions, namelist groups, derived-type definitions, procedure interface blocks and procedure definitions.

Fortran 2003 Standard

There are two types of modules, intrinsic and nonintrinsic. XL Fortran provides intrinsic modules, while nonintrinsic modules are user-defined.

An intrinsic module can have the same name as other global entities, such as program units, common blocks, external procedures, critical sections, or binding labels of global entities. A scoping unit must not access both an intrinsic module and a non-intrinsic module with the same name.

End of Fortran 2003 Standard
IBM Extension

Modules define global data, which, like COMMON data, is shared across threads and is therefore thread-unsafe. To make an application thread-safe, you must declare the global data as THREADPRIVATE or THREADLOCAL. See COMMON, THREADLOCAL, and THREADPRIVATE for more information.

End of IBM Extension
Read syntax diagramSkip visual syntax diagram>>-MODULE_statement--------------------------------------------><
 
Read syntax diagramSkip visual syntax diagram>>-+--------------------+--------------------------------------><
   '-specification_part-'
 
Read syntax diagramSkip visual syntax diagram>>-+------------------------+----------------------------------><
   '-module_subprogram_part-'
 
Read syntax diagramSkip visual syntax diagram>>-END_MODULE_statement----------------------------------------><
 
MODULE_statement
See MODULE for syntax details
specification_part
is a sequence of statements from the statement groups numbered  2 ,  4 , and  5  in Order of statements and execution sequence
module_subprogram_part:
Read syntax diagramSkip visual syntax diagram>>-CONTAINS_statement------------------------------------------><
 
Read syntax diagramSkip visual syntax diagram   .-------------------.
   V                   |
>>---module_subprogram-+---------------------------------------><
 
CONTAINS_statement
See CONTAINS for syntax details
END_MODULE_statement
See END for syntax details

A module subprogram is contained in a module but is not an internal subprogram. Module subprograms must follow a CONTAINS statement, and can contain internal procedures. A module procedure is defined by a module subprogram or an entry in a module subprogram.

Executable statements within a module can only be specified in module subprograms.

The declaration of a module function name of type character cannot have an asterisk as a length specification.

specification_part cannot contain statement function statements, ENTRY statements, or FORMAT statements, although these statements can appear in the specification part of a module subprogram.

Automatic objects and objects with the AUTOMATIC attribute cannot appear in the scope of a module.

An accessible module procedure can be invoked by another subprogram in the module or by any scoping unit outside the module through use association (that is, by using the USE statement). See USE for details.

IBM Extension

Integer pointers cannot appear in specification_part if the pointee specifies a dimension declarator with nonconstant bounds.

All objects in the scope of a module retain their association status, allocation status, definition status, and value when any procedure that accesses the module through use association executes a RETURN or END statement. See point 4 under Events causing undefinition for more information.

End of IBM Extension

A module is a host to any module procedures, interface blocks, or derived-type definitions it contains, which can access entities in the scope of the module through host association.

A module procedure can be used as an actual argument associated with a dummy procedure argument.

The name of a module procedure is local to the scope of the module and cannot be the same as the name of any entity in the module, except for a common block name.

Migration Tips:

FORTRAN 77 source:

      COMMON /BLOCK/A, B, C, NAME, NUMBER
      REAL A, B, C
      A = 3
      CALL CALLUP(D)
      PRINT *, NAME, NUMBER
      END
      SUBROUTINE CALLUP (PARM)
        COMMON /BLOCK/A, B, C, NAME, NUMBER
        REAL A, B, C
        ...
        NAME = 3
        NUMBER = 4
      END

Fortran 90/95/2003 source:

        MODULE FUNCS
          REAL A, B, C            ! Common block no longer needed
          INTEGER NAME, NUMBER    ! Global data
          CONTAINS
             SUBROUTINE CALLUP (PARM)
               ...
               NAME = 3
               NUMBER = 4
             END SUBROUTINE
        END MODULE FUNCS
        PROGRAM MAIN
        USE FUNCS
        A = 3
        CALL CALLUP(D)
        PRINT *, NAME, NUMBER
        END

Example of a module

MODULE M
  INTEGER SOME_DATA
  CONTAINS
    SUBROUTINE SUB()                      ! Module subprogram
      INTEGER STMTFNC
      STMTFNC(I) = I + 1
      SOME_DATA = STMTFNC(5) + INNER(3)
      CONTAINS
        INTEGER FUNCTION INNER(IARG)      ! Internal subprogram
          INNER = IARG * 2
        END FUNCTION
    END SUBROUTINE SUB
END MODULE
PROGRAM MAIN
  USE M                                   ! Main program accesses
  CALL SUB()                              !   module M
END PROGRAM