Visible to Intel only — GUID: GUID-C4CD092B-1B5F-4D10-935D-3FA18886EE25
Visible to Intel only — GUID: GUID-C4CD092B-1B5F-4D10-935D-3FA18886EE25
OPTIONAL
Statement and Attribute: Permits dummy arguments to be omitted in a procedure reference.
The OPTIONAL attribute can be specified in a type declaration statement or an OPTIONAL statement, and takes one of the following forms:
Type Declaration Statement:
type,[att-ls,] OPTIONAL [, att-ls] :: d-arg[, d-arg]...
Statement:
OPTIONAL [::] d-arg[, d-arg] ...
type |
Is a data type specifier. |
att-ls |
Is an optional list of attribute specifiers. |
d-arg |
Is the name of a dummy argument. |
Description
The OPTIONAL attribute can only appear in the scoping unit of a subprogram or an interface body, and can only be specified for dummy arguments. It cannot be specified for arguments that are passed by value.
A dummy argument is "present" if it associated with an actual argument that is itself present. A dummy argument that is not optional must be present. You can use the PRESENT intrinsic function to determine whether an optional dummy argument is associated with an actual argument.
To call a procedure that has an optional argument, you must use an explicit interface.
If argument keywords are not used, argument association is positional. The first dummy argument becomes associated with the first actual argument, and so on. If argument keywords are used, arguments are associated by the keyword name, so actual arguments can be in a different order than dummy arguments. A keyword is required for an argument only if a preceding optional argument is omitted or if the argument sequence is changed.
Example
The following example shows a type declaration statement specifying the OPTIONAL attribute:
SUBROUTINE TEST(A)
REAL, OPTIONAL, DIMENSION(-10:2) :: A
END SUBROUTINE
The following is an example of the OPTIONAL statement:
SUBROUTINE TEST(A, B, L, X)
OPTIONAL :: B
INTEGER A, B, L, X
IF (PRESENT(B)) THEN ! Printing of B is conditional
PRINT *, A, B, L, X ! on its presence
ELSE
PRINT *, A, L, X
ENDIF
END SUBROUTINE
INTERFACE
SUBROUTINE TEST(ONE, TWO, THREE, FOUR)
INTEGER ONE, TWO, THREE, FOUR
OPTIONAL :: TWO
END SUBROUTINE
END INTERFACE
INTEGER I, J, K, L
I = 1
J = 2
K = 3
L = 4
CALL TEST(I, J, K, L) ! Prints: 1 2 3 4
CALL TEST(I, THREE=K, FOUR=L) ! Prints: 1 3 4
END
Note that in the second call to subroutine TEST, the second positional (optional) argument is omitted. In this case, all following arguments must be keyword arguments.
The following shows another example:
SUBROUTINE ADD (a,b,c,d)
REAL a, b, d
REAL, OPTIONAL :: c
IF (PRESENT(c)) THEN
d = a + b + c + d
ELSE
d = a + b + d
END IF
END SUBROUTINE
Consider the following:
SUBROUTINE EX (a, b, c)
REAL, OPTIONAL :: b,c
This subroutine can be called with any of the following statements:
CALL EX (x, y, z) !All 3 arguments are passed.
CALL EX (x) !Only the first argument is passed.
CALL EX (x, c=z) !The first optional argument is omitted.
Note that you cannot use a series of commas to indicate omitted optional arguments, as in the following example:
CALL EX (x,,z) !Invalid statement.
This results in a compile-time error.