Declares the name, arguments, and code that form the body of a Function procedure.
[Public | Private] Function name [(arglist)] [ As classname ]
[name = expression]
[name = expression]
statement syntax has these parts:
argument has the following syntax and parts:
||Indicates that the argument is passed by value.
||Indicates that the argument is passed by reference.
||Name of the variable representing the argument; follows standard variable naming conventions.
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
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
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
, 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
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.
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
procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.