Chapter 9
Essentials of Essentials of Fortran-90/95/2003
Modules and Interfaces

Math 60 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 60 Homepage | Return to Brief Contents Page]
[Return to Full Contents Page]

Contents


9.1 Overview

Fortran-90 introduced the concept of a MODULE, not part of earlier versions of Fortran. This is a program unit that is not executed on its own, but may contain variable declarations, parameter definitions, subprograms (cf. Chapter 10), and definitions of new data types. In addition, certain components of a MODULE may be declared PUBLIC or PRIVATE, explicitly permitting or disallowing access to those components from outside of the MODULE.

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.

9.2 Simple Example

In the following example, a MODULE is defined and given the name MATHCONSTANTS. Within it PI and E are declared as parameters and given appropriate values.

    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.

9.3 Common Uses

Some of the major ways that MODULEs may be used are as follows: The following is an example of the third type of use, declaring variables in a MODULE that will be accessed by several program segments:
   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.

9.4 Public and Private Attributes

By default, all items in a MODULE are "public," that is, they are accessible when the MODULE is imported via a USE statement. If desired, certain items may be designated as "private." Normally, this is done by switching the default to private for all items by including the single statement
      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.

9.5 Additional Features

A USE statement can have alternative forms, with various specifiers. For example, one may specify which data elements one wishes to use from a MODULE. As an example,
      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

9.6 Overview of Interfaces

INTERFACEs have been introduced into Fortran-90/95 to provide needed information to a compiler in special circumstances. In the most general form, interfaces are included within a program segment or module in this way:
      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.