Lahey/Fujitsu Fortran

OPTIONAL Statement


The OPTIONAL statement declares that any dummy arguments specified need not be associated with an actual argument when the procedure is invoked.


OPTIONAL [::] dummy-args

dummy-args is a comma-separated list of dummy argument names.


A OPTIONAL dummy argument is tested for presence by using the PRESENT Function.

An optional dummy argument must not be referenced unless it has been tested and found to be present.

An interface is required before any procedure that has optional arguments can be called.

An optional argument may not be an actual argument if the corresponding dummy argument is not optional.


module mod1 ! provides implicit interface contains function fun1(a,b) real :: fun1,a real,optional :: b ! optional attribute if(present(b)) then ! don't reference b unless fun1=a+b ! it is present else fun1=a end if end function end module program present use mod1 write(*,*) fun1(2.) ! no optional argument write(*,*) fun1(2.,2.) ! optional argument end program

See Also

PRESENT Function