Share via


FUNCTION Command

Creates a user-defined function in a program file. There are two versions of the syntax.

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

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

Parameters

  • FUNCTION FunctionName
    Designates the beginning of a user-defined function and specifies the name of the function. FunctionName can contain up to 254 characters.

  • [ LPARAMETERS parameter1 [, parameter2] , ... ]
    Assigns data from the calling program to local variables or arrays. You can also use the PARAMETERS keyword instead of LPARAMETERS to accept privately scoped parameters. You can pass a maximum of 26 parameters to a function.

    For more information, see LPARAMETERS Command and PARAMETERS Command.

  • ( [ parameter1[ AS para1type][ , parameter2[ AS para2type] ],...] )
    Assigns data from the calling program to local variables or arrays. You can use the AS para1type clause to specify the data type of the variable.

    Note

    Including the parameters inside parentheses (()) immediately following the function name indicates that the parameters are locally scoped to the function.

  • [ AS returntype]
    Specifies the data type of the return value.

    You can use the AS clause to implement strong typing. For more information, see How to: Implement Strong Typing for Class, Object, and Variable Code.

  • Commands
    Specifies the Visual FoxPro commands to execute when executing the function.

  • [ RETURN [ eExpression] ]
    Returns control to the calling program or to another program. eExpression can specify a return value.

    Note

    You can include the RETURN command anywhere in the function to return control to the calling program or to another program and to define a value returned by the function. If you do not include the RETURN command, Visual FoxPro executes an implicit RETURN automatically when the function quits. If the RETURN command does not include a return value or if an implicit RETURN is executed, Visual FoxPro assigns True (.T.) as the return value. For more information, see RETURN Command.

  • [ ENDFUNC ]
    Indicates the end of the FUNCTION structure.

    Note

    The ENDFUNC keyword is optional because the function quits when it encounters another FUNCTION command, a PROCEDURE command, or the end of the program file. You cannot include normal executable program code following user-defined functions in a program file. Only other user-defined functions, procedures, and class definitions can follow the first FUNCTION or PROCEDURE command in the file.

Remarks

By default, parameters are passed to functions by value. For information about passing parameters to functions by reference, see SET UDFPARMS Command.

When you issue the DO command with a function name, Visual FoxPro searches for the function in the following order:

  1. The file containing the DO command.

  2. Procedure files opened with SET PROCEDURE.

    For more information, see SET PROCEDURE Command.

  3. Program files in the execution chain.

    Visual FoxPro searches program files in order from the most recently executed program to the first program executed.

  4. A standalone program file.

If a matching program file is found, Visual FoxPro executes the program. Otherwise, Visual FoxPro generates an error message.

To execute a function in a specific file, include the IN clause in the DO command.

For information about using the FUNCTION command when creating classes, see DEFINE CLASS Command.

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

Reference

PROCEDURE Command

PARAMETERS( ) Function

Other Resources

Commands (Visual FoxPro)