LF Fortran 95

Procedure Arguments

Arguments provide a means of passing information between a calling procedure and a procedure it calls. The calling procedure provides a list of actual arguments. The called procedure accepts a list of dummy arguments.

Argument Intent

Because Fortran passes arguments by reference, unwanted side effects can occur when an actual argument's value is changed by the called procedure. To protect the program from such unwanted side effects, the INTENT attribute is provided. A dummy argument can have one of the following attributes:

The INTENT attribute is specified for dummy arguments using the INTENT statement or in a type declaration statement.

Keyword Arguments

Using keyword arguments, the programmer can specify explicitly which actual argument corresponds to which dummy argument, regardless of position in the actual argument list. To do so, specify the dummy argument name along with the actual argument, using the following syntax:

 	

keyword = actual-arg

Where:

keyword is the dummy argument name.

actual-arg is the actual argument.

 	

Example:

... interface subroutine zee(a,b,c) integer :: a,b,c end subroutine end interface ... call zee(c=1, b=2, a=3) ...

In the example, the actual arguments are provided in reverse order.

A procedure reference can use keyword arguments for zero, some, or all of the actual arguments (see Optional Arguments below). For those arguments not having keywords, the order in the actual argument list determines the correspondence with the dummy argument list. Keyword arguments must appear after any non-keyword arguments.

Note that for a procedure invocation to use keyword arguments an interface must be available (see Procedure Interfaces).

Optional Arguments

An actual argument need not be provided for a corresponding dummy argument with the OPTIONAL attribute. To make an argument optional, specify the OPTIONAL attribute for the dummy argument, either in a type declaration statement or with the OPTIONAL statement.

An optional argument at the end of a dummy argument list can simply be omitted from the corresponding actual argument list. Keyword arguments must be used to omit other optional arguments, unless all of the remaining arguments in the reference are omitted. For example,

    subroutine zee(a, b, c)
       implicit none
       real, intent(in), optional :: a, c
       real, intent(in out) :: b
       ...
    end subroutine zee
 

In the above subroutine, a and c are optional arguments. In the following calls, various combinations of optional arguments are omitted:

    call zee(b=3.0)        ! a and c omitted, keyword necessary
    call zee(2.0, 3.0)     ! c omitted
    call zee(b=3.0, c=8.5) ! a omitted, keywords necessary
 

It is usually necessary in a procedure body to know whether or not an optional argument has been provided. The PRESENT intrinsic function takes as an argument the name of an optional argument and returns true if the argument is present and false otherwise. A dummy argument or procedure that is not present must not be referenced except as an argument to the PRESENT function or as an optional argument in a procedure reference.

Note that for a procedure to have optional arguments an explicit interface must be present (see Procedure Interfaces). Many of the Fortran intrinsic procedures have optional arguments.

Alternate Returns (obsolescent)

A procedure can be made to return to a labeled statement in the calling subprogram using an alternate return. The syntax for an alternate return dummy argument is

 	

*

The syntax for an alternate return actual argument is

 	

* label

Where:

label is a labeled executable statement in the calling subprogram.

An argument to the RETURN statement is used in the called subprogram to indicate which alternate return in the dummy argument list to take. For example,

    ...
    call zee(a,b,*200,c,*250)
    ...
 
    subroutine zee(a, b, *, c, *)
      ...
      return 2       ! returns to label 250 in calling procedure
      ...
      return 1       ! returns to label 200 in calling procedure
      return         ! normal return
 

Dummy Procedures

A dummy argument can be the name of a procedure that is to be referenced in the called subprogram or is to appear in an interface block or in an EXTERNAL or INTRINSIC statement. The corresponding actual argument must not be the name of an internal procedure or statement function.