As I was walking up the stair 
I met a man who wasn't there. 
He wasn't there again today. 
I wish, I wish he'd stay away. 

Hughes Mearns (1875-1965)

Up through Fortran 77, there was no Fortran standard-conforming way of calling a routine with a different number of arguments than it was declared as having. This didn’t stop people from omitting arguments, but whether or not it worked was highly implementation and argument dependent. For example, you can often get away with omitting numeric scalar arguments but not CHARACTER or arguments used in adjustable array bounds expressions, as code in the called routine’s “prologue” tries to reference the missing data, often resulting in an access violation (see Don’t Touch Me There.)

Fortran 90 introduced the concept of optional arguments and a standard-conforming way of omitting said optional arguments. Many users eagerly seized upon this and started using the new feature, but soon got tripped up and confused because they didn’t follow all of the rules the standard lays out. The Doctor is here to help.

First things first. To be able to omit an argument when calling a routine, the dummy argument in the called routine must be given the OPTIONAL attribute. For example:

SUBROUTINE WHICH (A,B)
INTEGER, INTENT(OUT) :: A
INTEGER, INTENT(IN), OPTIONAL :: B

If an argument has the OPTIONAL attribute, you can test for its presence with the PRESENT intrinsic. The standard prohibits you from accessing an omitted argument, so use PRESENT to test to see if the argument is present before touching it. That part is simple.

The part that people tend to miss, though, is that the use of OPTIONAL arguments means that an explicit interface for the routine is required to be visible to the caller. Generally, this means an INTERFACE block (which must match the actual routine’s declaration), but this rule is satisfied if you are calling a CONTAINed procedure. If you don’t have an explicit interface, the compiler doesn’t know that it has to pass a n “I’m not here” value (usually an address of zero) for the argument being omitted, and you could get an access violation or wrong results.

An interesting aspect of OPTIONAL arguments is that it’s ok to pass an omitted argument to another routine (which declares the argument as OPTIONAL) without first checking to see if it is PRESENT. The “omitted-ness” is passed along and can be tested by the other routine. What’s even more interesting is that the standard allows you to pull this trick on intrinsics such as MAX, PRODUCT, etc.!

There are some additional aspects of optional arguments, such as the use of keyword names in argument lists, that are worth learning about. For more information, see the section “Optional Arguments” in the Language Reference Manual. Another very important reference is the Language Reference Manual, “Determining When Procedures Require Explicit Interfaces.”. The Doctor highly recommends this for your reading pleasure. There will be a quiz next week (just kidding!).

(From Intel Developer Zone, copied with permission)

Write Your Comments

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Doctor Fortran

Subscribe to Doctor Fortran

Loading