Functions

A simple FUNCTION takes the form:

type FUNCTION name(list of arguments)
argument types
declaration of local variables
body of function
END [FUNCTION [name] ]

For example:

REAL FUNCTION mod(x,y)
REAL x,y
mod=SQRT(x*x+y*y) !SQRT is a standard intrinsic function
END FUNCTION mod

The body of the function contains one or more assignment statements in which the name of the function is assigned a value. If flow control statements and/or constructs are used within the body of the function then a RETURN statement can be used to exit from the function. The END statement implies RETURN.

As an alternative to using the function name in assignment statements, a different name may be used if this name is added to the specification as illustrated here:

REAL FUNCTION mod(x,y) RESULT(f)
REAL x,y
f=SQRT(x*x+y*y)
END FUNCTION mod

A function that either directly or indirectly calls itself must use RESULT and must be given the RECURSIVE attribute. For example:

RECURSIVE INTEGER FUNCTION factorial(n) RESULT(f)
INTEGER n
IF(n==0)THEN
 f=1
ELSE
 f=factorial(n-1)
ENDIF
END FUNCTION factorial

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited