Skip to content

Procedure interfaces

Tim Greaves edited this page Jun 18, 2014 · 5 revisions

Coding rules

Length of argument lists

Function and subroutine calls with many arguments are difficult to understand and cause many programming errors. Procedure interfaces should therefore be kept as short as possible. As a guideline, most procedures should have less than half a dozen arguments and very few procedures should have more than about a dozen. See the tutorial below for ways to reduce the length of argument lists.

Argument intent

Every single dummy argument of every single procedure must have declared intent. Declared argument intent is a form of documentation of arguments, improves compiler optimisation and enables automatic error checking of argument use. The sole exception to this are arguments with the pointer attribute as these are not permitted to have declared intent in Fortran 95 (this is fixed in Fortran 2003).

Explicit interfaces

Explicit interfaces facilitate compiler error checking and enable the use of Fortran features such as assumed shape arrays. The simplest and most reliable way of ensuring that a procedure always has an explicit interface where it is called is to place it in a module.

(Optional) arguments

If an argument is declared with the keyword optional, its function can be called with or without that argument. There are several coding rules for subroutine arguments:

  • Arguments to subroutines should not be declared as optional if all instances of calls to that subroutine have that argument.
  • If optional arguments exist in a subroutine call and a new required argument has to be added, it should be added before the optional arguments as a required argument. It should not be added at the end of the argument list and declared as optional (as it is required).
  • If an optional argument is really required (i.e. there is more than one call to that subroutine, one with the argument, one without) then a check should be made that it is present before using it.
  • New arguments to subroutines should be declared at the start of the subroutine - not interspersed amongst the local variable declarations. Furthermore they should be added in the same order they appear in the subroutine call.

Tutorial

Argument intent

Dummy arguments of prcedures can have declared intent. This is a way of specifying which variables are purely used to convey data into the procedure, which are defined by the procedure and which carry data into the procedure and are then modified. The following table shows the relationship between arguments of different intent:

Argument intent Argument defined on entry? Argument may be modified in procedure?
intent(in) Yes No
intent(out) No Yes
intent(inout) Yes Yes

Intent is an attribute which applies only to dummy arguments. Arguments with the pointer attribute are not permitted to have the intent attribute. The following example shows the use of all three argument intents:

subroutine delete_value(list, val, stat)
  ! Subroutine to find val in list and replace it with zero. 
  ! If stat is present, stat returns 1 if val is not in list and 0 otherwise.
  integer, dimension(:), intent(inout) :: list
  integer, intent(in) :: val
  integer, intent(out), optional :: stat

  integer :: loc

  ! Test for failure
  if (.not.any(list==val)) then
    if (present(stat)) stat=1
    return
  end if

  ! Flag successful completion
  if (present(stat)) stat=0

  ! Find the location of val in list.
  loc = minloc(list, mask=(list==val))

  list(loc)=0

end subroutine delete_value
Clone this wiki locally