LF Fortran 95

FUNCTION Statement

Description

The FUNCTION statement begins a function subprogram. It specifies the functions name and dummy arguments, and any special characteristics such as PURE, ELEMENTAL, or RECURSIVE. It may optionally specify the functions' return type, and the name of a result variable used within the subprogram to assign a value to the function result.

Syntax

[func-type] [type-spec] FUNCTION func-name ([dummy-args]) [RESULT(result-name)]
Where:
func-type is PURE, ELEMENTAL or RECURSIVE

type-spec is

INTEGER [kind-selector] or REAL [kind-selector] or DOUBLE PRECISION or COMPLEX [kind-selector] or CHARACTER [char-selector] or LOGICAL [kind-selector] or TYPE (type-name)

kind-selector is ([KIND=] kind)

char-selector is ( [LEN=] length [, ) KIND=kind])

or (KIND=kind [, LEN=length]) or * char-length [,]

kind is a scalar INTEGER expression that can be evaluated at compile time

length is a scalar INTEGER expression or *

char-length is a scalar INTEGER literal constant or (*)

func-name is the name of the function

dummy-args is a comma-separated list of dummy argument names

result-name is the name of the result variable

Remarks

A function with the prefix PURE or ELEMENTAL is subject to the additional constraints of pure procedures, which ensure that no unseen side effects occur on invocation of the function. PURE Procedure

An ELEMENTAL function is subject to the constraints of elemental procedures. ELEMENTAL Procedure

A function cannot be both ELEMENTAL and RECURSIVE.

The keyword RECURSIVE must be present if any function defined by a FUNCTION or ENTRY statement within the subprogram directly or indirectly calls itself.

A RECURSIVE function that calls itself directly must use the RESULT option.

If RESULT is omitted, then func-name is the result variable.

If the function result is an array or a pointer, this must be specified in the declaration of the result variable in the function body.

Example 1

! basic function declaration function func1(a,b) real :: func1 ! result type defined here real,intent(in) :: a,b func1=a-b ! function is assigned a result end function ! function declaration with result variable function func2(a,b) result(res) real :: res ! result type defined here real,intent(in) :: a, b res=a-b ! function is assigned a result end function ! function declaration with type real function func3(a,b) ! result type defined here real, intent(in) :: a,b func3=a-b ! function is assigned a result end function ! program invoking functions write(*,*) func1(-1.,1.) ! writes -2. write(*,*) func2(-1.,1.) ! writes -2. write(*,*) func3(-1.,1.) ! writes -2. end

Example 2

! recursive function with result variable recursive function func4(a,b) result(res) real :: res ! result type defined here real :: a,b if (a >= b) then res=a-b ! function returns else if (a < spacing(b)) then res=-b ! function returns else if (a < 0.) then b=func5(-a,b) ! indirect recursion else a=func4(a,-b) ! direct recursion end if end function ! recursive function without result variable recursive function func5(a,b) real :: func5 ! result type defined here real :: a,b if (a < b) then func5=func4(b,a) ! result variable not required else ! if recursive function does func5=func4(a,b) ! not invoke itself directly end if end function ! program invoking functions write(*,*) func4(-1.,1.),func4(1.,-1.) ! writes -1. 2. write(*,*) func5(-1.,1.),func5(1.,-1.) ! writes 2. 2. end