HP Fortran Programmer's Reference (September 2007)

HP Fortran statements
OPTIONAL (statement and attribute)
Chapter 10 417
The OPTIONAL attribute may be specified only for dummy arguments. It may occur in a
subprogram and in any corresponding interface body.
An optional dummy argument whose actual argument is not present may not be
referenced or defined (or invoked if it is a dummy procedure), except that it may be passed
to another procedure as an optional argument and will be considered not present.
When an argument is omitted in a procedure reference, all arguments that follow it must
use the keyword form.
If a procedure has an optional argument, the procedure interface must be explicit.
Examples
The following are two examples of the OPTIONAL statement. In the first example, the call to
the subroutine trip can legally omit the path argument because it has the OPTIONAL
attribute:
CALL TRIP ( distance = 17.0 ) ! path is omitted
SUBROUTINE trip ( distance, path )
OPTIONAL distance, path
In the next example, the subroutine plot uses the PRESENT function to determine whether or
not to execute code that depends on the presence of arguments that have the OPTIONAL
attribute:
SUBROUTINE plot (pts, o_xaxis, o_yaxis, smooth)
TYPE (point) pts
REAL, OPTIONAL :: o_xaxis, o_yaxis
! Origin - default (0.,0.)
LOGICAL, OPTIONAL :: smooth
REAL ox, oy
IF (PRESENT (o_xaxis)) THEN
ox = o_xaxis
ELSE
ox = 0.
! Note that the o_xaxis dummy argument cannot be referenced if
! the actual argument is not present. The same applies
! to o_yaxis (below).
END IF
IF (PRESENT (o_yaxis)) THEN
oy = o_yaxis
ELSE
oy = 0.
END IF
IF (PRESENT(smooth)) THEN
IF (smooth) THEN
... ! Smooth algorithm
RETURN
END IF