Share via


FUNCTION Command

Identifies the start of a definition for a user-defined function.

FUNCTION FunctionName 
   [ LPARAMETERS parameter1 [ ,parameter2 },... ]
   Commands 
   [ RETURN [ eExpression ] ]
ENDFUNC

Or

FUNCTION FunctionName( [ parameter1 [ AS para1type ][ ,parameter2 [ AS para2type ] ],...] ) [ AS returntype ]
   Commands 
   [ RETURN [ eExpression ] ]
ENDFUNC

Parameters

  • FUNCTION
    Indicates the beginning of the FUNCTION structure.
  • FunctionName
    Specifies the name of the user defined function. In Visual FoxPro, function names can be up to 254 characters long.
  • LPARAMETERS parameter1 [ ,parameter2 },...]
    Assigns data passed from the calling program to local variables or arrays.
  • ( [ parameter1 [ AS para1type ][ ,parameter2 [ AS para2type ] ],...] )
    Assigns data passed from the calling program to local variables or arrays. The optional clause AS para1type specifies the data type of the variable.
  • AS returntype
    Specifies the data type that the returned value is based on.
  • Commands
    Specifies the Visual FoxPro commands that are executed when the function is executed.
  • RETURN [ eExpression ]
    Returns control to the calling program or to another program. The optional parameter eExpression can be used to specify a value to return.
  • ENDFUNC
    Indicates the end of the FUNCTION structure.

Remarks

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:

  1. Visual FoxPro searches the file containing the DO command.
  2. If the function isn't found there, Visual FoxPro searches the open procedure files. Procedure files are opened with SET PROCEDURE.
  3. 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.
  4. 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 26 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.

Example

This example creates a custom object class called Hello and adds a function method called SayHello. The 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

See Also

LPARAMETERS | PARAMETERS | PARAMETERS( ) | PRIVATE | PROCEDURE | PUBLIC | RETURN | SET PROCEDURE | SET UDFPARMS