The FORTRAN Programming Language 

 

Abstraction and Encapsulation

COMMON Blocks

In pre-90's FORTRAN, all references to non-local variables were through COMMON blocks. These blocks were encapsulations of data that could be passed from one function to another as needed. The same variable could belong to several COMMON blocks. One unnamed COMMON block is allowed. The rest must be designated.

The block is defined where the variables are declared:

COMMON [/<name>/] <list of variables to share>

Then in the external subprogram, local variables are mapped to local variables:

COMMON [/<name>/] <list of local variables of correct types>

Though the names of the local variables in the external subprogram do not have to match the original names, the types of the variables do. Arrays may also be passed in COMMON blocks along with variables for their dimensions. But this requires that an array of exactly the same dimensions of the COMMON array to already have been locally declared.

There are a few occasions where COMMON blocks are necessary, however they should be avoided whenever parameter passing is an option.

MODULES and Dynamic Array Allocation

FORTRAN 90 has included MODULE to allow blocks of parameters, variables, and subprograms to be both shared and re-used. The basic structure is as follows:

MODULE <name>

<specifications>

END MODULE <name>

 

This module can then be passed to another program through USE. An optional command ONLY limits the parts of the module that can be referenced.

USE <name> [ONLY : <partial list of variables in module>]

Modules also allow for the passing of arrays whose size is not specified in the program or subprogram referencing them. The Array itself is defined as ALLOCATABLE in a module, along with a variable for it's size. The values in that module can be used to later allocate the array, reference the array in programs and subprograms, and deallocate the array. See the following site for an example of a program that does this.

http://www.edu.ph.unito.it/f90_tutorial/modules.html

If modules are to be included in a program or subprogram, the USE statement must be the first statement in the declaration section.

 

User Derived Types

FORTRAN 90 adds the ability to create user-defined types to its capabilities. The declaration structure is fairly straightforward.

TYPE <object name>
<declarations>
END TYPE <object name>

The statement to declare an instance of a user-defined object is:

TYPE (<object name>) :: <name>

The values stored and assigned in the variables of a derived type can be referenced with the '%' operator.

x = y%<variable>

y%<variable> = 0

Derived types can be member variables of other derived types and referenced with a string of '%' operators.

 

Example of Derived Type, instantiation, and assignment:  

TYPE Point
REAL :: x, y
END TYPE Point

 

TYPE Circle
TYPE (point) :: Center
REAL :: Radius
END TYPE Circle

 

TYPE Circle :: round

 

round%Radius = 10.
round%Center%x = 0
round%Center%y = 10