Skip to main content

FUNCTION

Defines an external function.

Synopsis

FUNCTION name[(arglist)] 
    [statements]
    RETURN(returnval) 

Arguments

name Name of the FUNCTION; follows standard variable naming conventions.
arglist Optional — List of variables specifying arguments that are passed to the FUNCTION procedure when it is called. Multiple arguments are separated by commas. The arglist is enclosed with parentheses.
statements A group of statements to be executed within the body of the FUNCTION procedure.
returnval Return value of the FUNCTION. If no return value is specified, FUNCTION returns the empty string.

Description

The FUNCTION statement defines an external function that returns a value to the invoking procedure. This FUNCTION procedure is visible to all other procedures in your script. The values of local variables in a FUNCTION are not preserved between calls to the procedure.

The FUNCTION statement is very similar to SUBROUTINE, except that FUNCTION returns a value. Like a SUBROUTINE procedure, a FUNCTION procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a SUBROUTINE procedure, you can use a FUNCTION procedure on the right side of an expression in the same way you use any intrinsic function.

There cannot be a label on the FUNCTION statement line. The FUNCTION statement must be the first line in the external function, with the following exceptions: comment lines, $OPTIONS statements, $COPYRIGHT statements, and DIM statements that do not dimension a static array. For example, DIM Var() and DIM abc are permitted, but DIM Var(2) is not.

There can only be one FUNCTION statement in an external function (no nested functions). You can't define a FUNCTION procedure inside another FUNCTION or inside a SUBROUTINE procedure.

Before invoking a function, it must be locally defined using the DEFFUN statement.

Examples

The following two examples show the definition of a function and the invocation of that function:

FUNCTION IsGreaterThan(lower, upper)
IF lower < upper 
THEN RETURN(1)
ELSE RETURN(0)
DEFFUN IsGreaterThan(x,y)
CRT IsGreaterThan(x,y)

See Also

FeedbackOpens in a new tab