Purpose
The OPTIONAL attribute specifies that a dummy argument need not be associated with an actual argument in a reference to the procedure.
Format
>>-OPTIONAL--+----+--dummy_arg_name_list----------------------->< '-::-' |
Rules
A reference to a procedure that has an optional dummy argument specified must have an explicit interface.
Use the PRESENT intrinsic function to determine if an actual argument has been associated with an optional dummy argument. Avoid referencing an optional dummy argument without first verifying that the dummy argument is present.
A dummy argument is considered present in a subprogram if it is associated with an actual argument, which itself can also be a dummy argument that is present (an instance of propagation). A dummy argument that is not optional must be present; that is, it must be associated with an actual argument.
An optional dummy argument that is not present may be used as an actual argument corresponding to an optional dummy argument, which is then also considered not to be associated with an actual argument. An optional dummy argument that is not present is subject to the following restrictions:
The OPTIONAL attribute cannot be specified for dummy arguments in an interface body that specifies an explicit interface for a defined operator or defined assignment.
Attributes Compatible with the OPTIONAL Attribute | ||||
---|---|---|---|---|
|
Examples
SUBROUTINE SUB (X,Y) INTERFACE SUBROUTINE SUB2 (A,B) OPTIONAL :: B END SUBROUTINE END INTERFACE OPTIONAL :: Y IF (PRESENT(Y)) THEN ! Reference to Y conditional X = X + Y ! on its presence ENDIF CALL SUB2(X,Y) END SUBROUTINE SUBROUTINE SUB2 (A,B) OPTIONAL :: B ! B and Y are argument associated, IF (PRESENT(B)) THEN ! even if Y is not present, in B = B * A ! which case, B is also not present PRINT*, B ELSE A = A**2 PRINT*, A ENDIF END SUBROUTINE
Related Information