Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 9/08/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Optional Arguments

Dummy arguments can be made optional if they are declared with the OPTIONAL attribute. In this case, an actual argument does not have to be supplied for it in a procedure reference.

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.

Positional arguments (if any) must appear first in an actual argument list, followed by keyword arguments (if any). If an optional argument is the last positional argument, it can simply be omitted if desired.

However, if the optional argument is to be omitted but it is not the last positional argument, keyword arguments must be used for any subsequent arguments in the list.

Optional arguments must have explicit procedure interfaces so that appropriate argument associations can be made.

The PRESENT intrinsic function can be used to determine if an actual argument is associated with an optional dummy argument in a particular reference.

A dummy argument or an entity that is host associated with a dummy argument is not present if any of the following are true for the dummy argument:

  • It does not correspond to an actual argument.

  • It corresponds to an actual argument that is not present.

  • It does not have the ALLOCATABLE or POINTER attribute, and corresponds to one of the following:

    • An actual argument that has the ALLOCATABLE attribute and is not allocated

    • An actual argument that has the POINTER attribute and is disassociated

The following example shows optional arguments:

  PROGRAM RESULT
  TEST_RESULT = LGFUNC(A, B=D)
  ...
  CONTAINS
    FUNCTION LGFUNC(G, H, B)
      OPTIONAL H, B
      ...
    END FUNCTION
  END

In the function reference, A is a positional argument associated with required dummy argument G. The second actual argument D is associated with optional dummy argument B by its keyword name (B). No actual argument is associated with optional argument H.

The following shows another example:

   ! Arguments can be passed out of order, but must be
   !  associated with the correct dummy argument.
   CALL EXT1 (Z=C, X=A, Y=B)
   . . .
   END

   SUBROUTINE EXT1(X,Y,Z)
      REAL X, Y
      REAL, OPTIONAL :: Z
      . . .
   END SUBROUTINE

In this case, argument A is associated with dummy argument X by explicit assignment. Once EXT1 executes and returns, A is no longer associated with X, B is no longer associated with Y, and C is no longer associated with Z.

If you pass an omitted dummy argument as the actual argument to a procedure, the corresponding dummy argument is considered to be omitted as well. This rule applies to both intrinsic and non-intrinsic procedures. For example:

CALL SUB1()
CONTAINS
  SUBROUTINE SUB1(B)
  LOGICAL, OPTIONAL :: B
  PRINT *, INDEX('Fortran','r',BACK=B) ! Prints 3
  CALL SUB2(B) ! Same as CALL SUB2()
  END SUBROUTINE SUB1

  SUBROUTINE SUB2(C)
  LOGICAL, OPTIONAL :: C
  PRINT *, PRESENT(C) ! Prints F
  END SUBROUTINE SUB2
END

See Also