Skip to main content

Function

Declares the name, arguments, and code that form the body of a Function procedure.

Synopsis

[Public | Private] Function name [(arglist)] [ As classname ]
    [statements]
    [name = expression]
    [Exit Function] 
    [statements]
    [name = expression]
End Function 

Arguments

The Function statement syntax has these parts:

Public Optional — Keyword indicating that the Function procedure is accessible to all other procedures in all scripts.
Private Optional — Keyword indicating that the Function procedure is accessible only to other procedures in the script where it is declared.
name Name of the Function. Follows local variable naming conventions.
arglist Optional — List of variables representing arguments that are passed to the Function procedure when it is called, separated by commas.
classname Optional — Name of the class of the return value.
statements Any group of statements to be executed within the body of the Function procedure.
expression Optional — Return value of the Function.

The arglist argument has the following syntax and parts:

[ByVal | ByRef] varname[( )]

ByVal Indicates that the argument is passed by value.
ByRef Indicates that the argument is passed by reference.
varname Name of the variable representing the argument; follows standard variable naming conventions.

Description

Function procedures are visible to all other procedures in your script. The value of local variables in a Function is not preserved between calls to the procedure.

All executable code must be contained in the procedure. Nesting is not permitted; you cannot define a Function procedure inside another Function or Sub procedure.

The Exit Function statement causes an immediate exit from a Function procedure. Program execution continues with the statement following the statement that called the Function procedure. Any number of Exit Function statements can appear anywhere in a Function procedure.

Like a Sub 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 Sub procedure, you can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Sqr, Cos, or Chr, when you want to use the value returned by the function.

You call a Function procedure using the function name, followed by the argument list in parentheses, in an expression. See the Call statement for specific information on how to call Function procedures.

There are two ways to return a value from a function: you can specify the value on a Return statement, or you can assign the value to the function name. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to name, the procedure returns a default value: a zero-length string (""). A function that returns an object reference returns a zero-length string ("") if no object reference is assigned to name within the Function.

Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.

All variables in a Caché Basic Function procedure are private. Therefore, a Function procedure cannot access public variables, such as SQLCODE. To use public variables, use a top-level Caché Basic routine, rather than a called function or subroutine.

To omit an arglist argument value, you must specify an undefined variable. This is a significant difference between ObjectScript and Caché Basic. In ObjectScript an omitted argument can be specified using a placeholder comma. In Caché Basic you cannot use a placeholder comma; you must supply an undefined named variable.

Examples

The following example shows both ways to assign a return value. First by specifying “True” to the Return statement, then by assigning “False” to the function named IsGreaterThan. False is assigned to the function name to indicate that an invalid value was found.

Function IsGreaterThan(lower, upper)
If lower < upper Then Return True
IsGreaterThan = False
End Function 

Notes

Function procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.

See Also

FeedbackOpens in a new tab