Partager via


PROCEDURE Command

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

PROCEDURE ProcedureName 
   [ LPARAMETERS parameter1 [ ,parameter2 ] ,... ]
      Commands 
   [ RETURN [ eExpression ] ]
[ENDPROC]

PROCEDURE ProcedureName( [ parameter1 [ AS para1type ][ ,parameter2 
[AS para2type ] ] ,...] ) [ AS returntype ]
      Commands 
   [ RETURN [ eExpression ] ]
[ENDPROC]

Parameters

  • PROCEDURE ProcedureName
    Designates the beginning of a user-defined procedure and specifies the name of the procedure. ProcedureName must begin with a letter or underscore and can contain up to 254 characters with any combination of letters, numbers, and underscores.

  • [ 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 procedure.

    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 procedure name indicates that the parameters are locally scoped to the procedure.

  • [ 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 procedure to return control to the calling program or to another program and to define a value returned by the procedure. If you do not include the RETURN command, Visual FoxPro executes an implicit RETURN automatically when the procedure 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.

  • [ ENDPROC ]
    Indicates the end of the PROCEDURE structure.

    Note

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

Remarks

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

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

  1. The file containing the DO command.

  2. The current database.

  3. Procedure files opened with SET PROCEDURE.

    For more information, see SET PROCEDURE Command.

  4. Program files in the execution chain.

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

  5. 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 procedure in a specific file, include the IN clause in the DO command.

For information about use of the PROCEDURE command when creating classes, see DEFINE CLASS Command.

Example

The following example illustrates how a procedure can be called to accomplish a discrete task such as making an entry in a log file. The procedure opens the log file (which is assumed to exist in the example), constructs an entry based in information passed in parameters, writes the entry out, and closes the file. The procedure is called with a DO command similar to the one at the top of the program.

DO MakeLogEntry WITH "Logged in", "jsmith"

PROCEDURE MakeLogEntry
 PARAMETERS message, username
 pnHandle = FOPEN("LOG2.TXT",2)     && Assume the file exists
 pnSize = FSEEK(pnHandle,0,2)           && Move to end of file
 logEntry = dtoc(date())+","+time()+","+username+","+message
 =FPUTS(pnHandle, logEntry)
 =FCLOSE(pnHandle)  && Close file
ENDPROC

The following example shows how a procedure can be called to return a value.

SET CENTURY ON
? longdate(({^1998-02-16}))  && Displays Monday, February 16, 1998

PROCEDURE longdate
 PARAMETER mdate
 RETURN CDOW(mdate) + ", " + MDY(mdate)
ENDPROC

See Also

Reference

FUNCTION Command

PARAMETERS( ) Function

SYS(16) - Executing Program File Name

Other Resources

Commands (Visual FoxPro)