Identifies the start of a definition for a user-defined function.
FUNCTION FunctionName [LPARAMETERS parameter1[,parameter2},...] Commands [RETURN [eExpression]] ENDFUNC
FUNCTION FunctionName([parameter1[,parameter2},...]) Commands [RETURN [eExpression]] ENDFUNC
- In Visual FoxPro, function names can be up to 254 characters long.
To distinguish a program file name with more than 10 characters from a function beginning with the same 10 characters in these two products, surround the program file name with quotation marks or include an extension after the program file name.
In many programs, certain routines are frequently repeated. Defining commonly used routines as separate functions reduces program size and complexity and facilitates program maintenance.
FUNCTION FunctionName is a statement within a program. It designates the beginning of a function in a program and identifies the function by name.
FUNCTION FunctionName is followed by a series of Visual FoxPro commands that make up the function. You can include RETURN anywhere in the function to return control to the calling program or to another program, and to define a value returned by the user-defined function. If you do not include a RETURN command, an implicit RETURN is automatically executed when the function quits. If the RETURN command does not include a return value (or if an implicit RETURN is executed), Visual FoxPro assigns .T. (True) as the return value.
The function ends with the ENDFUNC command. This command is optional; the function quits when it encounters another FUNCTION command, a PROCEDURE command, or the end of the program file.
Comments can be placed on the same line after FUNCTION and ENDFUNC. These comments are ignored during compilation and program execution.
You cannot have normal executable program code included in a program file after user-defined functions; only user-defined functions, procedures, and class definitions can follow the first FUNCTION or PROCEDURE command in the file.
When you issue DO with a function name, Visual FoxPro searches for the function in a specific order as follows:
- Visual FoxPro searches the file containing the DO command.
- If the function isn't found there, Visual FoxPro searches the open procedure files. Procedure files are opened with SET PROCEDURE.
- If the function isn't found in an open procedure file, Visual FoxPro searches the programs in the execution chain. Program files are searched from the most recently executed program through the first program executed.
- If the function is still not found, Visual FoxPro searches for a stand-alone program. If a matching program file is found, the program is executed. Otherwise, Visual FoxPro generates an error message.
Include the IN clause in DO to execute a function in a specific file.
By default, parameters are passed to functions by value. For information on passing parameters to functions by reference, see SET UDFPARMS. A maximum of 27 parameters can be passed to a function. Parameters can be passed to a function by including a PARAMETERS or LPARAMETERS statement in the function, or by placing a list of parameters immediately after FUNCTION FunctionName. Enclose the list of parameters in a set of parentheses, and separate the parameters with commas.
For information about use of the FUNCTION command in creating classes, See DEFINE CLASS and Strong Typing in Class, Objects, and Variable code in Help.
This example creates a custom object class called
Hello and adds a function method called
SayHello method returns the character string "Hello World", which is displayed by the MESSAGEBOX function. Note: The class definition code is placed after the program code that instantiates the object.
Local oHello oHello=CREATEOBJECT("Hello") =MESSAGEBOX(oHello.SayHello(),48) RELEASE oHello * Class definition code DEFINE CLASS Hello AS CUSTOM FUNCTION SayHello RETURN "Hello World" ENDFUNC ENDDEFINE