Identifies optional arguments for procedures.
Syntax
The syntax of the type declaration statement with the OPTIONAL
attribute is:
type , attrib-list :: dummy-argument-name-list |
- type
is a valid type specification (INTEGER,
REAL, LOGICAL,
CHARACTER, TYPE
( name),
etc.).
- attrib-list
is a comma-separated list of attributes including
OPTIONAL and
optionally those attributes compatible with it, namely:
- dummy-argument-name-list
is a comma-separated list of dummy-argument-names.
The syntax of the OPTIONAL
statement is:
OPTIONAL [::] dummy-argument-name-list |
Description
If a dummy argument has the OPTIONAL
attribute, the corresponding actual argument need not appear in
a procedure reference. In cases where there are arguments that generally
do not change from one reference to another, it is convenient to
specify that the arguments are optional and provide default values
for them. They can then be omitted from references in these general
cases. The presence of an optional argument in a procedure may be
determined by using the PRESENT
intrinsic function.
Many uses of the ENTRY
statement in FORTRAN 77 programs can be replaced by the use of optional
arguments.
The following restrictions apply to the use of the OPTIONAL
attribute:
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 END IF ... ! Plot points END SUBROUTINE plot ! Some valid calls to plot. CALL plot (points) CALL plot (observed, o_xaxis = 100., o_yaxis = 1000.) CALL plot (random_pts, smooth = .TRUE.) |
Related statements
SUBROUTINE
and FUNCTION
Related concepts
For related information, see the following: