OPTIONAL arguments and PRESENT

A procedure usually has one required way to be called and several variations: a threshold that defaults, a flag that turns on a report, a name that is only sometimes known. Fortran writes those with optional: the dummy argument may be left out of the call, and present tells the procedure whether it arrived. This lesson writes the three shapes that cover almost every use.

An optional dummy

The attribute goes on the dummy argument of the procedure, not on the declaration of the caller: character(len=*), intent(in), optional :: punctuation. The call may then give the value or leave it out, and every call with the same procedure stays valid.

The file optional.f90 has a subroutine with two optional arguments and calls it three ways:

module greet implicit none contains subroutine welcome(name, punctuation, times) character(len=*), intent(in) :: name character(len=*), intent(in), optional :: punctuation integer, intent(in), optional :: times character(len=2) :: mark integer :: n, i if (present(punctuation)) then mark = punctuation else mark = "!" end if n = 1 if (present(times)) n = times do i = 1, n print '(a,a,a)', "hello ", trim(name), trim(mark) end do end subroutine welcome end module greet program optional_demo use greet, only: welcome implicit none call welcome("world") call welcome("world", "?") call welcome("world", "!", 2) end program optional_demo

gfortran -Wall -Wextra -std=f2018 optional.f90 -o optional
./optional

hello world!
hello world?
hello world!
hello world!

The first call leaves both optional arguments out, the second gives the punctuation, the third gives both. The name and the punctuation are declared with len=*, so each draw the length from the argument, and the print uses an (a,a,a) format because the pieces have different lengths.

PRESENT

present(x) is the only safe question to ask about an optional dummy. It is a logical function of the language, it may only be applied to a dummy argument, and it tells the truth at every call. Without it the procedure would read a variable that the caller did not supply, which the standard does not define.

A procedure may pass its optional argument on to another procedure, and the child procedure can ask present on its own dummy: an absent argument passed on stays absent. What a program must not do is pass an absent argument where the other procedure declares it required.

A default when it is absent

The commonest shape is a local variable, a test and a fallback: n = 1 then if (present(times)) n = times. The local has a value in both branches, and the rest of the subroutine reads it without another test. The alternative, an if block around every use, repeats the test and is worth it only when the absent case must do nothing at all.

A default written this way can be a value, an expression of the required arguments, or a call to another function. It cannot be an initialisation in the declaration: a dummy argument declared integer, optional :: n = 1 is not a default, it is a declaration with an initialisation, which the standard does not allow for a dummy at all.

An optional switch

A logical argument that defaults to false is a switch, and it reads well because the call says what it turns on and nothing else. The program below prints a line always and a second line only when the flag is present and true.

program optional_flag_demo implicit none call report(3) call report(3, .true.) contains subroutine report(n, verbose) integer, intent(in) :: n logical, intent(in), optional :: verbose logical :: loud loud = .false. if (present(verbose)) loud = verbose print *, "n =", n if (loud) print *, "verbose output on" end subroutine report end program optional_flag_demo

gfortran -Wall -Wextra -std=f2018 flag.f90 -o flag
./flag

n = 3
n = 3
verbose output on

The second call passes .true. by position, which is why the flag is the second dummy. A call that passes only the flag and not the number cannot be written by position, and that is where the keyword form of the next lesson comes in: call report(3, verbose=.true.) is the explicit version of the same call.

What an absent argument is not

An absent optional argument is not zero, not a blank string and not a copy of the previous call: it is simply not there, and using it in any expression is a mistake the compiler cannot always see. A build with -fcheck=all turns that mistake into a runtime message naming the argument, which is worth keeping on while a procedure with optional arguments is developed.

Two rules follow from it. A procedure tests present before the first use and not in every branch; a caller that wants a value supplies one rather than relying on a default it hopes for. A default that matters to the result of the computation belongs in the argument list as a value the caller can see.

Exercises

  1. Call welcome("world", times=3). Which punctuation does the output use?
  2. Make name optional too and print a different greeting when it is absent. Which test comes first?
  3. Remove the if (present(punctuation)) and use the argument directly. What happens at the first call, with and without -fcheck=all?
  4. Give report a second optional argument that prints a label before the number. Which order of dummies keeps the existing calls valid?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where is the OPTIONAL attribute written?

    Answer

    On the dummy argument in the procedure

    The attribute belongs to the dummy argument declaration.

  2. What does present(x) tell a procedure?

    Answer

    Whether the optional argument x was supplied at this call

    PRESENT asks about the presence of the argument.

  3. What does this program print? program p implicit none call s(2) contains subroutine s(n, m) integer, intent(in) :: n integer, intent(in), optional :: m integer :: k k = 0 if (present(m)) k = m print *, n + k end subroutine s end program p

    Answer

    2

    The absent argument leaves the default zero.

  4. What may a procedure do with an absent optional argument?

    Answer

    Nothing except test it with PRESENT

    An absent argument has no value to use.

  5. Which call is valid for subroutine s(a, flag) with flag optional?

    Answer

    call s(1)

    The trailing optional argument is simply left out.

  6. What happens when an absent optional argument is passed on to another optional dummy?

    Answer

    It stays absent, and the child can test PRESENT

    Absence travels with the argument.

  7. Why is integer, optional :: n = 1 not a default value?

    Answer

    A dummy argument cannot be initialised in its declaration

    Initialisation belongs to local variables, not dummies.

  8. What is the usual pattern for a default value?

    Answer

    A local set to the default, then an IF (PRESENT) that replaces it

    The local has a value in both branches.

  9. Why does call report(3, verbose=.true.) need the keyword?

    Answer

    The first argument is required, so the flag cannot be passed by position alone

    A keyword lets the call skip the earlier argument.

  10. Which build option reports a use of an absent optional argument at run time?

    Answer

    -fcheck=all

    The runtime checks include the presence of dummies.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image