HP Fortran Programmer's Reference (September 2007)

Program units and procedures
Arguments
Chapter 7 177
! use PRESENT intrinsic to see if i2 has an actual
! argument associated with it
IF (PRESENT(i2)) THEN
add_or_inc = i1 + i2 ! add both arguments
ELSE
add_or_inc = i1 + 1 ! increment required argument
END IF
END FUNCTION add_or_inc
END PROGRAM main
Here are the command lines to compile and execute the program, along with the output from
a sample run:
$ f90 optional_arg.f90
$ a.out
11
30
For information about the syntax, rules and restrictions governing the OPTIONAL statement
and attribute, see “OPTIONAL (statement and attribute)” on page 416. For information about
the PRESENT intrinsic see “PRESENT(A)” on page 604.
Duplicated association
If a procedure reference would cause a data object to be associated with two or more dummy
arguments, the object must not be redefined within the referenced procedure. Consider the
following example:
PROGRAM p
CALL s (a,a)
CONTAINS
SUBROUTINE s (c,d)
c = 22.01 ! ILLEGAL definition of one of the dummy
! arguments associated with data object a
...
END SUBROUTINE
END PROGRAM
Both dummy arguments, c and d, are associated with the actual argument a. The procedure
includes an assignment to c, the effect of which is to redefine a. This attempt to redefine a is
invalid. This rule actual arguments that are overlapping sections of the same array.
Similarly, if a data object is available to a procedure through both argument association and
either use, host, or storage association, then the data object must be defined and referenced
only through the dummy argument.