LF Fortran 95

Modules

Module program units provide a means of packaging anything that is required by more than one scoping unit (a scoping unit is a program unit, subprogram, derived type definition, or procedure interface body, excluding any scoping units it contains). Modules may contain type specifications, interface blocks, executable code in module subprograms, and references to other modules. The names in a module can be specified PUBLIC (accessible wherever the module is used) or PRIVATE (accessible only in the scope of the module itself). Typical uses of modules include:

The form of a module program unit is

 
module-stmt [use-stmts] [specification-part] CONTAINS [module-subprogram-part] end-stmt
Where:
module-stmt is a MODULE statement. use-stmts is one or more USE statements. specification-part is one or more interface blocks or specification statements other than OPTIONAL or INTENT. module-subprogram part is one or more module procedures. end-stmt is an END statement.
 	

Example:

module example implicit none integer, dimension(2,2) :: bar1=1, bar2=2 type phone_number !derived type definition integer :: area_code,number end type phone_number interface !explicit interfaces function test(sample,result) implicit none real :: test integer, intent(in) :: sample,result end function test function count(total) implicit none integer :: count real,intent(in) :: total end function count end interface interface swap !generic interface module procedure swap_reals,swap_integers end interface contains function swap_reals !module procedure ... end function swap_reals function swap_integers !module procedure ... end function swap_integers end module example

Module Procedures

Module procedures have the same rules and organization as external procedures. They are analogous to internal procedures, however, in that they have access to the data of the host module. Only program units that use the host module have access to the module's module procedures. Procedures may be made local to the module by specifying the PRIVATE attribute in a PRIVATE statement or in a type declaration statement within the module.

Using Modules

Information contained in a module may be made available within another program unit via the USE statement. For example,

use set_module

would give the current scoping unit access to the names in module set_module. If a name in set_module conflicts with a name in the current scoping unit, an error occurs only if that name is referenced. To avoid such conflicts, the USE statement has an aliasing facility:

use set_module, a => b

Here the module entity b would be known as a in the current scoping unit.

Another way of avoiding name conflicts, if the module entity name is not needed in the current scoping unit, is with the ONLY form of the USE statement:

use set_module, only : c, d

Here, only the names c and d are accessible to the current scoping unit.

Forward references to modules are not allowed in LF95. If a module resides in a separate file from the code that uses the module, the module must be compiled before the code using the module. If a module and the code using the module are in the same source file, the compiler will compile the module in the proper order, regardless of where the module appears in the source file.