Modules

Programs are made up of program units consisting of one main PROGRAM together with optional external procedures and MODULEs.

A MODULE can contain data that is to be shared by a number of program units. Variables can be declared within a MODULE and accessed and changed by any program unit that includes a corresponding USE statement. A MODULE supercedes the COMMON block of Fortran 77 and earlier standards.

A MODULE can also contain the definition of user-defined types together with the definition of procedures that are common to a number of program units. Some of these procedures may be used to create user-defined operators. In this respect, a MODULE can be designed to be "object oriented".

A MODULE has the form:

MODULE module_name
[specification_statements
]
[CONTAINS
module_procedures]
END [MODULE [module_name]]

The specification statements can include the definition of user-defined types and the declaration of variables of any known type. USE statements relating to other MODULEs can also be included.

The module procedures are presented in exactly the same way as in other program units.

 

The USE statement

A USE statement has the form:

USE module_name

A USE statement may appear in a main program, another module or a procedure (of any kind). Variables declared in a module are shared amongst all the program units using the module.

 

USE ONLY and renaming

ONLY may be included in a USE statement in the form:

USE module_name, ONLY: list_of_variables

For example:

USE MyModule, ONLY : nValues

The effect is to make all the variables in the given module unavailable except for those in the list of variables. This will avoid possible name clashes.

A local alias can be substituted for the name of a module variable as illustrated here:

USE MyModule, n=>nValues
USE MyModule, ONLY: n=>nValues

n is a local alias for the variable nValues in MyModule.

 

The PRIVATE and PUBLIC statements

The PRIVATE statement is used within a MODULE in order to specify that certain variables may only be used within the module (in internal procedures) and are not available when the module is USEd.

The form is:

PRIVATE list_of_variables

A PRIVATE statement without a list makes all variables PRIVATE by default. A PUBLIC statement is then used to specify those variables that may be USEd.

The form of the PUBLIC statement is:

PUBLIC list_of_variables

 

Compiling modules

Modules can be placed in a file together with other program units. However, it is recommended that modules be placed in their own files so that they can be separately compiled. If a module is changed in a way that does not affect the interface then program units that use that module do not need to be recompiled.

The following more advanced topics are not described here:

Generic procedures (i.e. overloading procedure names)

User-defined operators (including assignment)

Extending intrinsic generic functions

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited