Statement FUNCTION Foundation
Declares functions with name, formal parameter list and variables.
[STATIC] FUNCTION <FunctionName> [( <Parameters,...> )] [LOCAL <VarName_L> [ [:= <Expression>],...]] [STATIC <VarName_S> [ [:= <Expression>],...]] [MEMVAR <VarName_M,...>] [FIELD <FieldName,...> [IN <AliasName>]] <SourceCode> RETURN <returnValue>
The FUNCTION statement declares a block of program statements as a User-Defined Function (UDF). A UDF is executed when the function name <FunctionName>, followed by parentheses, is specified in the code. Within the parentheses, optional expressions may be supplied. The values of these expressions are passed to the function and assigned to the variables which were declared in the list of formal parameters <Parameters,...>. "Parameter" designates a variable to which a value is assigned by the calling of the function. The passed value is called a function argument. The value of a function parameter is visible within the function as a LOCAL variable.
Functions are used to structure a program and to organize the tasks of a program in components. The basic difference between functions and procedures is that functions have a defined return value, whereas procedures always return the value NIL, which means they have no return value. Functions are introduced by the FUNCTION statement, procedures by the PROCEDURE statement. Both delimit a block of program statements which is executed by using the symbolic name, followed by parentheses. The end of the block of program statements for a function is identified by a new FUNCTION, PROCEDURE, CLASS or METHOD statement or by the end of the source code file. It is considered good programming practice, however, to indicate the end of a function with the RETURN statement.
RETURN passes control back to the calling function or procedure. Multiple RETURN statements can be used within a function or procedure, but it is good programming practice to have only one - at the end of the function or procedure.
The visibility of UDFs can be restricted by the addition of the STATIC keyword. A STATIC FUNCTION is visible only within the PRG file in which it is declared.
User-defined functions follow the same syntactical rules as the Xbase++ functions. They can be called with no argument, multiple arguments or with all declared arguments. When arguments are not passed, the value of the corresponding function parameters within the function call equal NIL. Any validation of the function parameters must be done inside the UDF. A UDF can also be called with an alias operator in front, causing a specific work area to be selected before execution of the function. Arguments are passed to a function by value. When an argument is to be passed by reference, the reference operator @ should be placed in front of the name of the applicable variable. Only character, numeric, logical and date variables can be passed by reference with the reference operator. Arrays and objects are automatically passed by reference.
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.