previous up next print clean
Next: Intrinsics Up: FORTRAN90 BASICS Previous: Structures

Modules

Modules are the key unifying element among the new tools in Fortran90. They are also the thing that replaces the most confusing parts of Fortran77. In particular, common blocks, the scourge of old Fortran programs brought on by the need for global variables without passing excessive numbers of arguments, can be completely done away with. A module provides an explicit interface to any program or subprogram that uses it, which is to say that whatever data structures and/or subroutines are placed in a module are universally available, and in such a way that usage can be checked by the compiler to prevent the easy corruption that is a risk of common blocks.

A Fortran90 module consists of two parts. The first, the declaration part, is for data structures and interfaces. As an example, the code fragment below contains a simple data structure consisting of an allocatable real-valued vector and an integer, which would presumably be used to hold the length of the vector. It also contains an tt interface, which are used to overload operators, functions, and subroutines. The interface is named myminus, which is the name that a calling program would use to access one of the two module procedures listed. These are subroutines or functions that are coded in the second part of the module. When compiling a main program which calls one of these two subroutines or functions, the compiler decides which of the two to use in the executable based on the arguments given to the subroutine. If the arguments in the calling program match the first module procedure, that is used in the executable, if the arguments match the second, then that is used. Or in other words, compile time rather than run time checking.

module mymodule
type myvector
	real, dimension(:), pointer :: vector
	integer                     :: length
end type myvector
interface myminus
	module procedure myminusvector, myminusscalar
end interface

The second part of the module is the subprogram part, which is where subroutines and functions are put. The subprogram part of the module begins with the word contains. Below is continued the example module, with two subroutines corresponding to the two module procedures listed in the first part of the module. The first multiplies a vector by negative one, the second multiplies a real number by negative one.

contains

subroutine myminusvector(inputvector)
type (myvector):: inputvector
inputvector%vector = inputvector%vector * -1.
end subroutine myminusvector

subroutine myminusscalar(inputscalar)
real		:: inputscalar
inputscalar = inputscalar * -1.
end subroutine myminusscalar

end module mymodule

It is the explicit interface, half created by the module, that allows the checking required to resolve which subroutine to call. The other half of the interface comes from the calling program, which must include the line use mymodule. Note that the potential for ugliness brought on by use-ing many modules from a given program is easily averted, because modules may use other modules, and thus be chained together. Our SEP Fortran90 library contains many modules, which are joined by use association in a single module called sep_f90_mods. Any program which simply uses this one module will have access to all the modules in the library.


previous up next print clean
Next: Intrinsics Up: FORTRAN90 BASICS Previous: Structures
Stanford Exploration Project
11/12/1997