Back to index.html.
Re-use of past programs is a good practice to reduce the development cost. Such programs should be an excellent shape to be easily used in other program units. External functions and subroutines are reusable, but the additional description of the correct interface is required. Also, there is an issue when some variables and constants should be shared with program units; there is a primitive syntax to do it, but it is old-fashioned and not recommended. Instead, it would be best if you used a module. A module in Fortran is a collection of variable, procedures, and interfaces which are easily used in any program units.
Definition of module
A Fortran module is a program unit (like
subroutine). The module is defined with the
module statement. The structure looks like the main program. Here is a small example of defining a module.
module mymod implicit none real,parameter :: pi=3.1415926 real :: number_of_traits=0 contains function mean(x) result(m) real,intent(in) :: x(:) real :: m m = sum(x)/size(x) end function mean end module mymod
It defines a constant
pi, a variable
number_of_traits (with the initial value is 0), and a function
mean. When using this module in the main program (or any program units), all the variables and the function will be immediately accessible. To use this module, you should use the
use statement, which should be at the beginning of the program unit.
program main ! at the top of program use mymod ! put implicit none and variables after the use statement implicit none real :: x(5)=[1,2,3,4,5] ! You can use anything defined in the module without any declaration. print *,sqrt(2*pi) number_of_traits = 10 print *,mean(x) end program main
You can see how a module is defined and how it is used.
- A module is defined with
- The module can contain any constants, variables, derived types, interfaces, and procedures. The procedures should be placed after
- The module looks like the main program, including only the declarations and the definitions.
- To use a module, put the
usestatement at the top of the program unit.
The procedures can access all variables defined in the module (above
contains). The module is useful to unify some variables and procedures that use the variables.
When a module is used in different program units, the variables in the module will be shared. The shared variable is also called a public variable. It means that using a variable in the module, when one program changes the variable, this change is effective in all programs that use the module. See the following example.
subroutine set_num_traits(n) use mymod integer,intent(in) :: n number_of_traits = n end subroutine set_num_traits program main use mymod interface subroutine set_num_traits(n) use mymod integer,intent(in) :: n end subroutine set_num_traits end interface print *,number_of_traits call set_num_traits(5) print *,number_of_traits end program main
The variables defined in a module are kind of global variables that can be universally accessible from all programs that use the module.
Order of compilation
A restriction of the module is that the module should be compiled before the programs that use the module. For example, the following program structure is required if all the program units are in the same file. This is because a compiler should perform the consistency checks in all the defined variables and procedures between the module and the program units that call it.
! The module comes first. module mymod ... end module mymod ! The program calling the module. program main use mymod end program main
If the order changed, the compiler could not find the information about the module, and the compilation error should occur.
You can see that a new file is created when a module is compiled. The file ends with
.mod which contains the details about this module. The compiler searches this file to check if the module has been already compiled. So, the file should stay in the same directory (or accessible directories by the compiler) as the source file.
- A module is a program unit, including variables, derived types, and procedures.
- The procedures can access to the variables defined in the module.
- When multiple program units import the same module, the variables in the module will be shared by all the program units.
- The module should be compiled prior to being used.
There is no exercise in this section.
Advanced usage of the module
By default, the
use statement imports all objects (variables and procedures) defined in the module. If you want to use a specific object, the
only option can do this job. The following example imports only
mean from the module.
program main use mymod, only:pi,mean ... end program main
The name of the imported object should not be the same as the variable names in the program unit. You can change the name of the imported object in
only as follows.
new_name => original_name
Here is an example of name change:
average refers to
program main use mymod, only:pi,average=>mean ... end program main
The selective import is always recommended when you use a module unless you need all the objects in the module.
Private and public objects
All the objects defined in a module can be imported by default. In other words, the objects have the implicit public attribute. You can make some objects private. The private objects are visible only within the module; the procedures in the module can access to such private objects.
private specify the attribute of objects. The statements should stay before
module mymod implicit none real,parameter :: pi=3.1415926 real :: number_of_traits=0 public :: pi,mean private :: number_of_traits contains function mean(x) result(m) real,intent(in) :: x(:) real :: m m = sum(x)/size(x) end function mean end module mymod
You can use the following simplified statements.
! All objects are public by default; only number_of_traits is private. public private :: number_of_traits
private attribute can also useful to declare a private variable.
module mymod implicit none real,parameter :: pi=3.1415926 real,private :: number_of_traits=0 ... end module mymod
usestatement imports all objects from the module. The
onlyoption imports specific objects, optionally with the name change.
- All the objects in a module are public by default. The
publicstatement makes the listed objects public explicitly. To make some of them private, use the
privatestatement or the
There is no exercise in this section.
As intrinsic functions like
sqrt(), there are several intrinsic modules defined in the Fortran standard. The following modules are available by default in recent compilers supporting Fortran 2003 or later.
||Definition of useful constants|
||Functions and constants needed to use external C functions|
||Treatment of special values in floating-point arithmetic|
You can simply use such modules as a regular module with
use. A better practice is to indicate the module is intrinsic using attribute
intrinsic; you have to use
:: to list the module name.
program main use, intrinsic :: iso_fortran_env ... end program main
Back to index.html.