



Variables as Dummy Arguments
SUBROUTINE SILLY(N, M) N = N + M END |
NUMBER = 10
CALL SILLY(NUMBER, 5)
|
CALL SILLY(10, 7) |
If you make use of procedures written by other people you may be worried about
unintentional effects of this sort. In principle it should be possible to prevent a
procedure altering a constant argument by turning each one into an expression, for
example like this:
CALL SILLY(+10, +5)
or
CALL SILLY((10), (5))
Although either of these forms should protect the constants, it is still against the
rules of Fortran for the procedure to attempt to alter the values of the corresponding
dummy arguments. You will have to judge whether it is better to break the rules of
the language than to risk corrupting a constant.
Expressions, Subscripts, and Substrings
If the actual argument contains expressions then these are evaluated before the
procedure starts to execute; even if the procedure later modifies operands of the
expression this has no effect on the value passed to the dummy argument. The same
rule applies to array subscript and character substring expressions. For example, if
the procedure call consists of:
CALL SUB( ARRAY(N), N, SIN(4.0*N), TEXT(1:N) )
and the procedure assigns a new value to the second argument, N, during its
execution, it has no effect on the other arguments which all use the original value of
N. The updated value of N will, of course, be passed back to the calling
unit.
Passed-length Character Arguments
A character dummy argument will have its length set automatically to that of the
corresponding actual argument if the special length specification of *(*) is
used.
To illustrate this, here is a procedure to count the number of vowels in a character string. It uses the intrinsic function LEN to determine the length of its dummy argument, and the INDEX function to see whether each character in turn is in the set "AEIOU" or not.
INTEGER FUNCTION VOWELS(STRING)
CHARACTER*(*) STRING
VOWELS = 0
DO 25, K = 1,LEN(STRING)
IF( INDEX('AEIOU', STRING(K:K)) .NE. 0) THEN
VOWELS = VOWELS + 1
END IF
25 CONTINUE
END
|
INTEGER statement in each
program unit which references the function.
This passed-length mechanism is recommended not only for general-purpose software where the actual argument lengths are unknown, but in all cases unless there is a good reason to specify a dummy argument of fixed length.
There is one restriction on dummy arguments with passed length: they cannot be
operands of the concatenation operator (//) except in assignment statements. Note
that the same form of length specification "*(*)" can be used for named character
constants but with a completely different meaning: named constants are not subject
to this restriction.