The module statement is the first statement in a module program unit. Inside of the module, different variables and subprogram units can be defined. The variables and procedures that are defined inside of a module can then be made available to different program units ( the main program, functions, subroutines and other modules ) via the USE association.
Up until FORTRAN 90 the only way for different program units to share variables was through their argument lists or through the use of common blocks. Even though some very serious and practical procedures can be written using these two features ( check out the example that Ellis provides in chapter 15.8 of Fortran 77 Programming ) the reality was that it often made for problems and code that could not be easily followed. Argument lists could get monstrously long and the programmer had to be careful when managing the rigid structure of common blocks. When it came time to write the FORTRAN 90 standard, the powers that be realized that it would be advantageous to do something to eliminate these problems. Plus, it became necessary to develop a new FORTRAN structure to compliment some of the other new features that they wanted to introduce with FORTRAN 90. For instance the problem arose on how to pass derived data types to subroutines and functions. Because of the rules governing the transfer of data through argument lists, the receiving program unit could have problems getting information on the data types associated with the derived type's component arguments. The solution to these problems and to several others was the creation of the FORTRAN module construct.
A module can provide full knowledge of and access to all of the variables that are contained within it via the use association. This cutbacks on the number of variables that need to be passed through argument lists and common blocks by clumping all global variables ( those that are important to a block of the program's components ) into a single module or a handful of modules.
Hopefully from this simple explanation of the origins and power of the module construct you are beginning to see its potential usefulness. Armed with this basic understanding of what a module is, you can now begin to explore some of its intimate details.
Let's start out defining the various applications of modules by showing how it can be used to provide access in different program units to the same variables. The first thing to do is to type your module declaration statement. This statement should include a name that you give to the module. For instance, I'm going to call the module in this example group_1. Its declaration statement will then look like this.
The name "group_1" must be unique, and can not match the name of any other program unit, common block, or variable contained within "group_1". The next thing would be to include the names and types of variables that are going to be stored in the module. So for this example I'm going to define the following variables.
real, allocatable, dimension(:,:) :: matrix1, matrix2 real pi, array(1:10) integer dim1, dim2, ldata,i character*25 file_name
I can also assign values to these variables within the module.
data pi,array / 3.1415926,10*1 /
The rest of the variables can then be left to get values assigned to them inside of the main program or some other program unit that uses this module. It is important to note that any variable defined in a module that gets a value assigned to it in some other program unit, will have that value associated with that variable in every other program unit that uses the same module.
To finish the module, the last thing that needs to be typed is an end card.
end module group_1
Any program unit can then have access to the variables contained in the module just as long as the program unit includes the following use statement.
A second important use of modules is to store internal functions and subroutines. You may want to consider placing any internal procedures needed by several units in a module to provide global accessibility. To include a procedure in module requires the use of the contains statement. The following example illustrates this use.
module my_stuff use group_1 implicit none contains function flow_rate(velocity,dia) implicit none real velocity, dia, area area = pi * (dia**2/4.0) flow_rate = velocity * area return end function flow_rate end module my_stuff
Before I finish my discussion on modules I just want to make sure that you realize that the variable pi is made available to the function flow_rate through the use of the module group_1 which I defined earlier. This is perfectly valid FORTRAN for two reasons. First, the FORTRAN 90 standard makes it possible for one module to use another one. Second, even though the function flow_rate is a separate program unit, since it is contained within the module my_stuff it has access to all of the variables that the function my_stuff has access to (and all of the variables in the program unit using module "my_stuff").
The above overview of modules should give you a basic understanding of modules so that you can begin writing some fairly sophisticated programs with FORTRAN 90. However, if you have progressed beyond the beginner stage, you may want to learn more about derived types, the contains statement, and the public and private attributes. The power of modules are complimented nicely by these other FORTRAN statements and it is worth learning a little bit about them.
examples: fall3.f and odeint.f
Written by Jason Wehr: email@example.com and Maintained by John Mahaffy : firstname.lastname@example.org