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.

NoteNote

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.

NoteNote

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.

NoteNote

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.

Expand imageRemarks

Expand imageExample

Expand imageSee Also


© , 1996-2020 • Updated: 11/10/20
Comment or report problem with topic