function ʃreturn_typeʅ function_name(ʃparameter1_type parameter1_name, ...,ʅ
ʃparameterN_type parameterN_nameʅ)
// statements
ʃfunction_name = return_value;ʅ
end;
The
function statement defines a
user-defined function with the name
function_name. Once defined, the function can be called in other user-defined functions or in procedures throughout your application.
Numeric expressions, string expressions,
objects (such as arrays and file handlers), and
function pointers can be passed to a user-defined function as arguments. These parameters are defined by specifying the variable type (e.g.,
parameter1_type) and the name of the variable (e.g.,
parameter1_name). If no type is specified, the variable will be considered numeric. Preceding the parameter type with the
optional keyword marks the parameter as an
optional parameter for which a caller does not need to supply a corresponding argument.
The names used in the parameter list of a function are local to the function. They may not be the same as names that are defined in any dictionary or in
PROC GLOBAL, but the names can be reused in other functions or procedures.
Numeric, string, and alphanumeric variables are local to the function. That is, if a variable is passed as an argument, its value in the rest of the application will not be changed by actions within the function (this is called "pass by value"). On the other hand,
objects (such as arrays and file handlers) passed as arguments refer to the source variable and interactions on the variable affect the source variable (this is called "
pass by reference"). If you want to pass a numeric or string variable by reference, you can use the
ref keyword to signify that changes made in the function should affect the source variable.
Other than arrays, parameters are defined the same way you would in other parts of logic. However, with arrays, you do not define the size of the array because the array will match the size of the array passed as an argument. By default, an array is one-dimensional. To specify more than one dimension, use parentheses with the number of dimensions specified using commas. For example, a three-dimensional array must include
(,,) after the parameter name. Within the function, the function
length returns the dimension sizes of the passed array.
Functions always return a value, either a numeric, alphanumeric, or string value. If
return_type is not specified, the function will by default return a numeric value. To assign the function's return value, assign a value to
function_name. If no return value is specified, the value
default or a blank string is returned, depending on the return type. The return value can also be specified using the
exit statement.
When using functions in logic, CSPro will check that the function call is valid at during compilation. However, advanced users may find it useful to program function calls where the function name is only known at runtime. You can use the
invoke function to execute such functions using runtime binding.