Part of the usefulness of MODULEs stems from a basic Fortran rule about program segments (i.e., the main program and any subprograms). Since Fortran permits separate compilation of the main program and any subprograms and the existence of each other's names is made visible during the second stage of compilation (i.e., the "linking" phase), variables (and other program elements, such as parameters or definitions of derived types) internal to any program segment are always considered "local." In other words, any variable or other program element declared or defined within one program segment is recognized only within that program segment and is completely invisible to any other program segment. ("Global" variable declarations, which are possible in C/C++ and some other languages, are not possible in Fortran, cf. section 2.2.)
If used, a MODULE is placed in a Fortran program file before the main program. It is given its own name and this name is used to access it in another program segment by means of the USE statement. The USE statement comes immediately after the segment header statement (i.e., PROGRAM, SUBROUTINE, or FUNCTION), and before the IMPLICIT NONE statement and any variable declarations.
The skeleton of a MODULE is very simple.
MODULE module_name
in this section one may define data types and
declare variables and parameters
CONTAINS
in this optional section, one includes
member subroutines and functions of this module.
END MODULE
The MODULE name may also follow on the END MODULE
line.
MODULE MATHCONSTANTS
REAL, PARAMETER :: PI = 3.141592654
REAL, PARAMETER :: E = 2.718281828
END MODULE
!
PROGRAM TEST2
USE MATHCONSTANTS
IMPLICIT NONE
WRITE(*,*) "Pi=",PI," e=",E
END PROGRAM
In this example, even though PI and E
(declared in MODULE MATHCONSTANTS) are not
declared in the main program, they are nevertheless accessible since the
MODULE in which they have been declared has been imported into
the main program via the USE MATHCONSTANTS statement.
MODULE ONE
IMPLICIT NONE
INTEGER A
END MODULE
!
PROGRAM TEST3B
USE ONE
IMPLICIT NONE
A = 1
WRITE(*,*) "A=",A
CALL TWO
WRITE(*,*) "A=",A
END PROGRAM TEST3B
!
SUBROUTINE TWO
USE ONE
A = 2
END SUBROUTINE TWO
Although the technicalities of a SUBROUTINE are not
covered until the next chapter, this example declares the variable
A in MODULE ONE, which is accessible both from
the main program and from SUBROUTINE TWO and is declared
in neither. The main program sets the value of A to 1
and print out that value, and then SUBROUTINE TWO resets
the value to 2. When the program control returns to the main
program after CALL TWO, the value of 2 is actually printed
out. In this example, the variable A has been declared
in a MODULE and every program segment which imports
that MODULE can change variable values and have the
new values accessible to any other program segment which
also imports the same MODULE.
PRIVATE
at the beginning of the MODULE and then designating
the components that will remain "public" explicitly. For example,
one can designate variables as public in this way:
REAL, PUBLIC :: X, Y, Z
To designate subprograms as "public" (or private), one
includes the names in the beginning section of the MODULE
even though the body of the code follows after the CONTAINS
statement. The following would be an example:
MODULE THREE
PRIVATE
REAL :: X, Y, Z
PUBLIC :: SUB1
CONTAINS
SUBROUTINE SUB1(A, B)
! body of subroutine
END SUBROUTINE
END MODULE
Components designated PRIVATE are inaccessible to any
program segment which USEs the MODULE, but still
remain accessible to subprograms within the MODULE.
USE DATASET3, ONLY: X, Y
would allow the code to access only X and Y
from MODULE DATASET3 even though the MODULE
might contain numerous other variables.
It is possible to rename variables being accessed from a MODULE. For example, if one wanted to make use of variable Y in MODULE DATASET4, but call it Z2 in the code that is importing the MODULE, one could use this form of the USE statement:
USE DATASET4, Z2 => Y
INTERFACE name -or- new_operator -or- blank
necessary "interface" statements
END INTERFACE
When, for example, subprograms are used in a program and are included as internal subprograms or within modules, the needed information is always available. For subprograms that are "external," that is, included in a separate file (or even in the same file after the main program) without other links to the calling program, needed information related to special features may not be known. In situations such as these, it is sometimes necessary to use an INTERFACE. Such an INTERFACE does not include a name, and appears in the variable declaration section of the calling program segment. (An INTERFACE used in this way conveys needed information in a way similar to a C/C++ "prototype" statement, or a Pascal "forward" statement.) For examples of use, see sections 17.6 and 17.8. (Also see section 10.5 for information when using "assumed-shape arrays" in subprograms and using an interface block.)
Interfaces may also be used to define new unary or binary operators by referring to separately written functions. In such a case, the initial line is:
INTERFACE OPERATOR ( .op_abbrev. )
where .op_abbrev. is a character string (surrounded
by periods) that will be used as a symbol for the new operator
(by being placed before or between appropriate variables or constants).
For further information, see section 16.3.
Finally, interfaces may be used to group similar functions and give them a "generic" name. These similar functions may have different types for their input parameters and different output data types but perform the same fundamental action. In such a case, the initial line is:
INTERFACE generic_name
where generic_name is the generic name for the
functions or subroutines headers included within the interface, subprograms
which differ based on the data type of the parameters or on the output
function value. For further information,
see section 12.3.
One should consult a fuller Fortran book for additional details on the various uses of interfaces.
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
© Copyright 1999-2005 Dennis C. Smolarski, S.J., All rights reserved.
Last changed: 8 June 2005.