Arguments

Unless optional arguments are used (see below) the number of actual arguments must be the same as the number of dummy arguments. Unless keywords are used (see below) the order of the arguments must be the same. In addition, the arguments will usually have the same type (and type attributes), the same shape and (with character strings) the same length.

 

The INTENT attribute

In Fortran all arguments are passed by reference. This means that the information passed is the address of the data concerned. Potentially any argument can be used either for input or for output. There is therefore the likelihood of side effects at the point of call when a dummy argument is inadvertently changed within a procedure.

The INTENT attribute limits the way in which an argument may be used within a procedure as follows:

INTENT(IN) The procedure must not change the argument.

INTENT(OUT) The argument is undefined on entry and may be given a value in the procedure.

INTENT(INOUT) The argument may be given a value in the procedure.

For example:

REAL FUNCTION mod(x,y)
REAL,INTENT(IN)::x,y
mod=SQRT(x*x+y*y)
END FUNCTION mod

It is recommended that INTENT be used with all arguments. If the PURE attribute is attached to a procedure then a compilation error occurs unless all of the arguments are marked as INTENT(IN).

 

Keyword arguments

The name of a dummy argument can be used as a keyword in the call of a procedure. For example:

PROGRAM Prog5
 REAL a
 READ *, a
 PRINT *, func(x=a)
CONTAINS
 REAL FUNCTION func(x)
 REAL x
 func=x*x+1.0
 END
END Prog5

By using keywords, the order of the arguments can be changed. Also arguments that are optional (see below) may be omitted. If a keyword is used or an optional argument omitted, all arguments that follow must use keywords.

External procedures that have keyword or optional arguments must also have an explicit interface (see the next topic).

 

Optional arguments

The attribute OPTIONAL can be attached to a dummy argument. OPTIONAL arguments may be omitted from the actual argument list. The intrinsic function PRESENT may be used within a procedure to test if an optional argument was present in the actual argument list. For example:

REAL FUNCTION func(x,y)
REAL x,z
REAL,OPTIONAL::y
IF(PRESENT(y))THEN
 z=y
ELSE
 z=1.0
ENDIF
func=x*x+z
END FUNCTION func

External procedures that have optional arguments must have an explicit interface.

 

Assumed shape arrays

A dummy argument that is an array may be declared without its shape. In this case the dummy argument will inherit the shape of the actual array argument. For example:

SUBROUTINE ProcessArray(arr)
REAL arr(:,:)
INTEGER m,n
m=SIZE(arr,DIM=1)
n=SIZE(arr,DIM=2)
. . .
END SUBROUTINE ProcessArray

 

Automatic arrays

An array that is local to a procedure can be declared to have a shape that depends on the current arguments. For example:

SUBROUTINE ProcessArray(arr)
REAL,INTENT(IN)::arr(:)
REAL a(SIZE(arr)) !Lower bound is one
a=arr !Take a copy of arr
. . .
END SUBROUTINE ProcessArray

The alternative is to make an explicit call to ALLOCATE.

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited