Visible to Intel only — GUID: GUID-D0870826-6038-4E2B-860D-A078BF0ADB50
Visible to Intel only — GUID: GUID-D0870826-6038-4E2B-860D-A078BF0ADB50
MODULE
Statement: Marks the beginning of a module program unit, which contains specifications and definitions that can be used by one or more program units.
MODULE name
[specification-part]
[CONTAINS
[module-subprogram
[module-subprogram]...] ]
END[ MODULE [name]]
name |
Is the name of the module. |
specification-part |
Is one or more specification statements, except for the following:
An automatic object must not appear in a specification statement. |
module-subprogram |
Is a function or subroutine subprogram that defines the module procedure. A function must end with END FUNCTION and a subroutine must end with END SUBROUTINE. A module subprogram can contain internal procedures. |
Description
If a name follows the END statement, it must be the same as the name specified in the MODULE statement.
The module name is considered global and must be unique. It cannot be the same as any local name in the main program or the name of any other program unit, external procedure, or common block in the executable program.
A module is host to any module procedures it contains, and entities in the module are accessible to the module procedures through host association.
A module must not reference itself (either directly or indirectly).
You can use the PRIVATE attribute to restrict access to procedures or variables within a module.
Although ENTRY statements, FORMAT statements, and statement functions are not allowed in the specification part of a module, they are allowed in the specification part of a module subprogram.
The following rules also apply to modules:
The specification part of a module must not contain IMPORT, ENTRY, FORMAT, executable, or statement function statements.
A variable, common block, or procedure pointer declared in a submodule implicitly has the SAVE attribute, which may be confirmed by explicit specification.
If a specification or constant expression in the specification-part of a module includes a reference to a generic entity, there must be no specific procedures of the generic entity defined in the submodule subsequent to the specification or constant expression.
Any executable statements in a module can only be specified in a module subprogram.
A module can contain one or more procedure interface blocks, which let you specify an explicit interface for an external subprogram or dummy subprogram.
A module can be extended by one or more program units called submodules. A submodule can in turn be extended by one or more submodules.
Example
The following example shows a simple module that can be used to provide global data:
MODULE MOD_A
INTEGER :: B, C
REAL E(25,5)
END MODULE MOD_A
...
SUBROUTINE SUB_Z
USE MOD_A ! Makes scalar variables B and C, and array
... ! E available to this subroutine
END SUBROUTINE SUB_Z
The following example shows a module procedure:
MODULE RESULTS
...
CONTAINS
FUNCTION MOD_RESULTS(X,Y) ! A module procedure
...
END FUNCTION MOD_RESULTS
END MODULE RESULTS
The following example shows a module containing a derived type:
MODULE EMPLOYEE_DATA
TYPE EMPLOYEE
INTEGER ID
CHARACTER(LEN=40) NAME
END TYPE EMPLOYEE
END MODULE
The following example shows a module containing an interface block:
MODULE ARRAY_CALCULATOR
INTERFACE
FUNCTION CALC_AVERAGE(D)
REAL :: CALC_AVERAGE
REAL, INTENT(IN) :: D(:)
END FUNCTION
END INTERFACE
END MODULE ARRAY_CALCULATOR
The following example shows a derived-type definition that is public with components that are private:
MODULE MATTER
TYPE ELEMENTS
PRIVATE
INTEGER C, D
END TYPE
...
END MODULE MATTER
In this case, components C and D are private to type ELEMENTS, but type ELEMENTS is not private to MODULE MATTER. Any program unit that uses the module MATTER can declare variables of type ELEMENTS, and pass as arguments values of type ELEMENTS.
This design allows you to change components of a type without affecting other program units that use the module.
If a derived type is needed in more than one program unit, the definition should be placed in a module and accessed by a USE statement whenever it is needed, as follows:
MODULE STUDENTS
TYPE STUDENT_RECORD
...
END TYPE
CONTAINS
SUBROUTINE COURSE_GRADE(...)
TYPE(STUDENT_RECORD) NAME
...
END SUBROUTINE
END MODULE STUDENTS
...
PROGRAM SENIOR_CLASS
USE STUDENTS
TYPE(STUDENT_RECORD) ID
...
END PROGRAM
Program SENIOR_CLASS has access to type STUDENT_RECORD, because it uses module STUDENTS. Module procedure COURSE_GRADE also has access to type STUDENT_RECORD, because the derived-type definition appears in its host.