Partager via


How to: Create Procedures and Functions

You can create procedures and functions programmatically.

To create a procedure

  1. Begin the procedure definition with the PROCEDURE command and the procedure name.

  2. To define parameters, on the second line of the procedure definition, include an LPARAMETERS or PARAMETERS statement with a list of parameters.

    -OR-

    On the same line as the PROCEDURE statement and immediately following the procedure name, include a parameter list enclosed in parentheses.

  3. On the following lines, include the code statements you want to execute in the procedure.

  4. End the procedure definition with the ENDPROC keyword.

For example, the following lines of code show a basic example of a procedure:

PROCEDURE myProcedure
   LPARAMETERS myPar1, myPar2
   myPar1 = myPar1 + myPar2
ENDPROC

-OR-

PROCEDURE myProcedure(myPar1, myPar2)
  myPar1 = myPar1 + myPar2
ENDPROC

By default, values pass to procedures by reference. Therefore, changes made to the passed values in the procedure are passed back to the calling program.

For more information, see PROCEDURE Command, PARAMETERS Command, and LPARAMETERS Command.

To create a function

  1. Begin the function definition with the FUNCTION command and the function name.

  2. To define parameters, on the second line of the procedure definition, include an LPARAMETERS or PARAMETERS statement with a list of parameters.

    -OR-

    On the same line as the FUNCTION statement and immediately following the function name, include a parameter list enclosed in parentheses.

  3. On the following lines, include the code statements you want to execute in the function.

  4. Include a RETURN statement when you want to return a value from the function and control of code execution to the calling program.

  5. End the procedure definition with the ENDFUNC keyword.

For example, the following lines of code show a basic example of a function:

FUNCTION myFunction
  LPARAMETERS myPar1, myPar2
  myFuncReturnValue = myPar1 + myPar2
  RETURN myFuncReturnValue
ENDFUNC

-OR-

FUNCTION myFunction(myPar1, myPar2)
  myFuncReturnValue = myPar1 + myPar2
  RETURN myFuncReturnValue
ENDFUNC

By default, values pass to user-defined functions by value. Therefore, changes made to the passed values in the function are not passed back to the calling program.

For more information, see FUNCTION Command, PARAMETERS Command, and LPARAMETERS Command.

See Also

Tasks

How to: Call Procedures and Functions

Concepts

Passing Data to Parameters

Returning Data from Procedures and Functions

User-Defined Procedures and Functions

Other Resources

Working with Procedures and Functions