SuivantPrec.Bas prec.BasNiv. sup.

9.5 Variables as Dummy Arguments 

If the dummy argument of a procedure is a variable and it has a value assigned to it within the procedure, then the corresponding actual argument can be: If, however, the dummy variable preserves its initial value throughout the execution then the actual argument can be any of these three forms above or alternatively: The reason for this restrictions is easy to see by considering the ways of calling the subroutine SILLY in the next example:
        SUBROUTINE SILLY(N, M)
        N = N + M
        END
If it is called with a statement such as:
        NUMBER = 10
        CALL SILLY(NUMBER, 5)
                                                                  
                                                                  
then the value of NUMBER will be updated to 15 as a result of the call. But it is illegal to call the function with a constant as the first argument, thus:
        CALL SILLY(10, 7)
because on exit the subroutine will attempt to return the value of 17 to the actual argument which was specified as the constant ten. The effects of committing such an error are system-dependent. Some systems detect the attempt to over-write a constant and issue an error message; others ignore the attempt and allow the program to continue; but some systems will actually go ahead and over-write the constant with a new value, so that if you use the constant 10 in some subsequent statement in the program you may get a value of 17. Since this can have very puzzling effects and be hard to diagnose, it is important to avoid doing this inadvertently.

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
Note that the function has a data type which is not the default for its initial letter so that it will usually be necessary to specify its name in a 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.

SuivantPrec.Bas prec.HautNiv. sup.