Condividi tramite


PROCEDURE Command

Identifies the beginning of a procedure in a program file.

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

or

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

Parameters

  • ProcedureName
    Specifies the name of the procedure to create.

Remarks

PROCEDURE is a statement within a program file that specifies the beginning of each procedure in the program file and defines the procedure name. Procedure names must begin with a letter or underscore and may contain any combination of letters, numbers, and underscores.

In Visual FoxPro for Windows, procedure names can be up to 254 characters long.

Comments can be placed on the same line after PROCEDURE and ENDPROC. These comments are ignored during compilation and program execution.

The PROCEDURE statement is followed by a series of commands that make up the procedure. You can include RETURN 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 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 procedure ends with the ENDPROC command. This command is optional; the procedure quits when it encounters another PROCEDURE command, a FUNCTION command, or the end of the program file.

Note   You cannot have normal executable program code included in a program file after procedures; only procedures, user-defined functions, and class definition can follow the first PROCEDURE or FUNCTION command in the file.

When you execute a procedure with DO ProcedureName, Visual FoxPro searches for the procedure in a specific order. Visual FoxPro searches:

  1. The program containing DO ProcedureName.
  2. The current database.
  3. The procedure files opened with SET PROCEDURE.
  4. Programs in the execution chain. It searches program files beginning with the most recently executed program and continuing back to the first executed program.
  5. A stand-alone program file. If a program file with the same name as the file name specified with DO is found, Visual FoxPro executes the program. If a matching program file name isn't found, Visual FoxPro generates an error message.

Include the IN clause in DO to execute a procedure in a specific file.

By default, parameters are passed to procedures by value. For information on passing parameters to procedures by reference, see SET UDFPARMS. A maximum of 26 parameters can be passed to a procedure. Parameters can be passed to a procedure by including a PARAMETERS or LPARAMETERS statement in the procedure, or by placing a list of parameters immediately after PROCEDURE ProcedureName. Enclose the list of parameters in a set of parentheses, and separate the parameters with commas.

For information about use of the PROCEDURE command in creating classes, see DEFINE CLASS and Strong Typing in Class, Objects, and Variable code in Help.

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

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