This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Handout Six November 3, 2011 1 Fortran 90 Modules So far you have only been introduced to one type of program unit, that is, the main program unit. At first, in the main program unit, you were shown how to write simple executable code and then later how to use a CONTAINS statement to include your own functions and subroutines. There is another form of program unit in Fortran 90 and it is called a MODULE . A module has a range of applications in Fortran 90 and it is good practice to use them. A module can be seen as a related collection of entities that can be made available to one or more other program units. In this case an entity could be data, functions or subroutines. So, for example, you could have a module that contains all the functions and subroutines you have written that relate to matrices. This matrix module could then be incorporated into any other program you write that makes use of matrices, this provides a neat and structured way of including code you have already written into new code. 1.1 The structure of a module Like a program unit a module has a typical structure. This structure is similar to the main program unit. MODULE <module name> <USE [other modules]> IMPLICIT NONE <Specification Section> CONTAINS <module procedure one> <module procedure two> : : <module procedure n > END MODULE <module name> A module has its own name like any other program unit. A module can also make use of any entities in other modules, so you could have a USE statement between the module header and the IMPLICIT NONE statement. A module has its own IMPLICIT NONE statement. In the specification part of a module you can declare any data that you want to be globally visible to all the procedures defined later in the module. 1 A module can contain functions and subroutines just like a main program unit by placing them between a CONTAINS statement and the END MODULE statement. Modules are compiled separately from the main program unit so you should put them in a separate file. You can group more than one module in a single file but in most cases it is sensible to use a separate file for each module. So, for example, you could call the matrix module matrix mod.f90 . 1.2 Making USE of a module The last section explained what a fortran MODULE is, explained its structure and how to write one in fortran 90 code. In this section you will learn how to USE a fortran module in your programs so you can gain access to its SUBROUTINES , FUNCTIONS and data. Any declared entities in a module that are intended to be used externally can be accessed by Fortran 90s USE statement. The USE statement can be included in any program unit, function or subroutine of a Fortran 90 code. Its syntax can be described in three forms....
View Full Document
- Fall '10