The USE statement gives a program unit accessibility to public entities in a module. It takes one of the following forms:
An entity in the only-list can also take the form:
Rules and Behavior
If the USE statement is specified without the ONLY option, the program unit has access to all public entities in the named module.
If the USE statement is specified with the ONLY option, the program unit has access to only those entities following the option.
If more than one USE statement for a given module appears in a scoping unit, the following rules apply:
If two or more generic interfaces that are accessible in a scoping unit have the same name, the same operator, or are both assignments, they are interpreted as a single generic interface. Otherwise, multiple accessible entities can have the same name only if no reference to the name is made in the scoping unit.
The local names of entities made accessible by a USE statement must not be respecified with any attribute other than PUBLIC or PRIVATE. The local names can appear in namelist group lists, but not in a COMMON or EQUIVALENCE statement.
The following shows examples of the USE statement:
MODULE MOD_A INTEGER :: B, C REAL E(25,5), D(100) END MODULE MOD_A ... SUBROUTINE SUB_Y USE MOD_A, DX => D, EX => E ! Array D has been renamed DX and array E ... ! has been renamed EX. Scalar variables B END SUBROUTINE SUB_Y ! and C are also available to this subrou- ... ! tine (using their module names). SUBROUTINE SUB_Z USE MOD_A, ONLY: B, C ! Only scalar variables B and C are ... ! available to this subroutine END SUBROUTINE SUB_Z ...
The following example shows a module containing common blocks:
MODULE COLORS COMMON /BLOCKA/ C, D(15) COMMON /BLOCKB/ E, F ... END MODULE COLORS ... FUNCTION HUE(A, B) USE COLORS ... END FUNCTION HUE
The USE statement makes all of the variables in the common blocks in module COLORS available to the function HUE.
To provide data abstraction, a user-defined data type and operations to be performed on values of this type can be packaged together in a module. The following example shows such a module:
MODULE CALCULATION TYPE ITEM REAL :: X, Y END TYPE ITEM INTERFACE OPERATOR (+) MODULE PROCEDURE ITEM_CALC END INTERFACE CONTAINS FUNCTION ITEM_CALC (A1, A2) TYPE(ITEM) A1, A2, ITEM_CALC ... END FUNCTION ITEM_CALC ... END MODULE CALCULATION PROGRAM TOTALS USE CALCULATION TYPE(ITEM) X, Y, Z ... X = Y + Z ... END
The USE statement allows program TOTALS access to both the type ITEM and the extended intrinsic operator + to perform calculations.