Previous | Table of Contents | Next |
The use Statement
The simple form of the use statement is just the keyword use followed by a module to be used. However, with the use statement, there are two ways to affect the way that names in a module are accessed by another program unit. The first is that the names used in the module may be changed in the program unit using the module. For example, in a subroutine using module math_module, the programmer may rename the variable e to the longer name logarithm_base with the use statement:
use math_module, logarithm_base => e
Any number of rename clauses may appear in the use statement, but any name in the module can be renamed only once.
The second way to affect the objects accessed in a module is to have an only clause in the use statement. In the program circle, only the constant pi is needed. It is possible to prevent other names in the module from conflicting with names in the program; this can be accomplished with the use statement:
use math_module, only : pi
If, in addition, it is desirable to use and rename the parameter e to logarithm_base, this can be done with the statement
use math_module, only : pi, logarithm_base => e
1.2.14.2. Procedures
There are two kinds of procedures: functions and subroutines. A function looks much like a Fortran program, except that it begins with the keyword function instead of the keyword program. Once written, a function is used just like the built-in functions to compute a value that may be used in any expression. A subroutine also looks like a program or a function, except that the first line begins with the keyword subroutine. A subroutine may be used to perform any computation and is invoked by executing a call statement.
Functions and subroutines whose first statements begin with the keyword recursive are permitted to call themselves directly or indirectly; recursion is used to write clear and simple programs for what might otherwise be difficult programming tasks.
Argument Intent
In Fortran, you may indicate the intent of use of each dummy argument of a subroutine or function unless it is a pointer or dummy procedure. The intent may be in, which means that the dummy argument cannot be changed within the procedure; it may be out, which means that the actual argument must not be used until given a value in the procedure and is usually used to pass a value back to the calling program; or it may be in out, which means that the dummy argument is expected both to receive an initial value from and return a value to the corresponding actual argument. Thus, for dummy arguments with intent out or in out, the corresponding actual argument must be a variable.
Keyword Arguments
With the use of keyword arguments, it is not necessary to put the arguments in the correct order, but it is necessary to know the names of the dummy arguments. Suppose series_sum (m, n) is a function that sums the integers from m to n, inclusive. Then, the statement
print *, series_sum (400, 700)
prints the value of 400 + 401 + + 700. The same computation may be made using the statement
print *, series_sum (n = 700, m = 400)
Optional Arguments
In the sample computation of an arithmetic series, a common occurrence would be that the value of m is 1. It is possible to indicate that certain arguments to a procedure are optional arguments in the sense that they do not have to be present when the procedure is called.
An optional argument must be declared to be such within the procedure; usually, there would be some statements within the procedure to test the presence of the optional argument on a particular call and perhaps do something different if it is not there. In the example, if the function series_sum is called without the argument m, the value 1 is used. To do this, the intrinsic function present is used:
function series_sum (m, n) result (series_sum_result) integer, optional, intent (in) :: m integer, intent (in) :: n integer :: series_sum series_sum_result = n * (n + 1) / 2 end if if (present (m)) then series_sum_result = series_sum_result - (m - 1) * m / 2 if (present(m)) then else end if end function series_sum
The result clause names the variable series_sum_result as the one to hold the function result returned to the calling program. It is required in a recursive function but is optional, otherwise.
This new version of the function can now be called with any of the following statements, all of which compute the same sum:
print *, series_sum (1, 700) print *, series_sum (n = 700) print *, series_sum (n = 700, m = 1) print *, series_sum (m = 1, n = 700)
Previous | Table of Contents | Next |