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 begins

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.

Fortran 2003 ends

IBM extension begins 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. IBM extension ends

Syntax

MODULE
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_subprogram_part
Read syntax diagramSkip visual syntax diagram
>>-CONTAINS_statement------------------------------------------><

Read syntax diagramSkip visual syntax diagram
   .-------------------.   
   V                   |   
>>---module_subprogram-+---------------------------------------><

MODULE_statement
See MODULE for syntax details.
specification_part
It is a sequence of statements from the statement groups numbered  2 ,  4 , and  5  in Order of statements and execution sequence.
CONTAINS_statement
See CONTAINS for syntax details.
END_MODULE_statement
See END for syntax details.

Rules

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 begins

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.

IBM extension ends

A module is a host to any module procedures, interface blocks, derived-type definitions it contains, Fortran 2008 beginsor its descendant submodulesFortran 2008 ends. Through host association, the module procedures, interface blocks, derived-type definitions, Fortran 2008 beginsand descendant submodulesFortran 2008 ends can access entities in the scope of the module.

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.

Fortran 2008 beginsIn specification_part, you can declare a module procedure interface body to specify the interface of a separate module procedure. See Example 2. You can later define the separate module procedure in one of the descendant submodules.Fortran 2008 ends

Migration Tips:

  • Eliminate common blocks and INCLUDE directives
  • Use modules to hold global data and procedures to ensure consistency of definitions
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 1

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 module m
  CALL sub()
END PROGRAM

Example 2 (Fortran 2008)

MODULE m
  INTERFACE                             
    ! A module procedure interface body that specifies the
    ! interface of a separate module subroutine
    MODULE SUBROUTINE sub(arg)
      INTEGER :: arg
    END SUBROUTINE

    ! A module procedure interface body that specifies the
    ! interface of a separate module function
    INTEGER MODULE FUNCTION func(a, b)
      INTEGER :: a, b
    END FUNCTION  
  END INTERFACE
END MODULE

Related information



Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us