program example implicit none call sub() call sub(10) call sub(10, 20) call sub(10, 20, 30) contains subroutine sub(a1, a2, a3) integer, optional :: a1, a2, a3 if (present(a1)) print *, "a1=", a1 if (present(a2)) print *, "a2=", a2 if (present(a3)) print *, "a3=", a3 end subroutine end
all three dummy arguments of sub are declared with the OPTIONAL attribute and all 4 calls to subroutine sub are valid. The output from the above program would be:
Note that the intrinsic procedure PRESENT is used in the callee to determine whether an actual argument was associated with the corresponding dummy argument.
Now suppose you want to call sub in the example above, but you only want to provide the first and the third argument without having to load a placeholder for the second argument. This can be achieved by using argument keywords in your procedure call:
call sub(a1=10, a3=30)
The output from this call alone would be:
Please note that a C function prototype that has variable argument list is not interoperable with Fortran.