This documentation is archived and is not being maintained.

Parameters in Procedures and Functions

Visual Studio 2005

When a program calls a procedure or function, it can pass data to the procedure or function for processing. However, when you create a procedure or function that performs operations on data that passes to it from the calling program, the procedure or function must contain parameters in its definition to handle the data. The following sections contain information about working with parameters:

Defining Parameters in Procedures and Functions

When a program passes data to a procedure or function, the parameters in the procedure or function handles that data so the procedure or function can perform operations on the data. When you want to create procedures and functions that process data that the calling program passes to them, you need to include parameters in the procedure and function definitions.

The following lines of code illustrate the basic format for including parameters in procedure definition:

PROCEDURE myProcedure
   LPARAMETERS Par1, Par2, Par3, ...
   * Insert procedure code.
ENDPROC

-OR-

PROCEDURE myProcedure(Par1, Par2, Par3, ...)
  * Insert procedure code.
ENDPROC

The first procedure includes an LPARAMETERS statement that can contain one or more parameters. The second procedure lists the same parameters enclosed by parentheses (()) immediately following the procedure name. Using the LPARAMETERS statement or enclosing the parameter list in parentheses defines the parameters with local scope for the procedure. You can also use the PARAMETERS keyword instead of LPARAMETERS to accept privately scoped parameters. You can pass multiple values to a procedure or function by separating the values with commas.

For example, the following examples show procedure definitions that include parameters. The first procedure includes an LPARAMETERS statement and two parameters, myPar1 and myPar2. The second procedure lists the same parameters enclosed by parentheses (()) immediately following the procedure name. Both procedures add the value in myPar2 to myPar1 and assign the result to myPar1. By default, data passes to procedures by reference, so the new value of myPar1 replaces its original value.

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

-OR-

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

You can include parameters similarly in a function definition. For more information, see PROCEDURE Command, FUNCTION Command, LPARAMETERS Command, and PARAMETERS Command.

Passing Data to Procedures and Functions

You can pass data, or specifically, "arguments", from your program to procedures and functions in different ways, depending on the following:

For example, when you call a procedure with the DO command, you can pass data using the WITH clause and a parameter list. In the following example, the variables myVar and myVar2 contain the values 4 and 5. When you call the procedure myProcedure using the DO command, the parameter list in the WITH clause passes the variables to the procedure:

myVar = 4
myVar2 = 5
DO myProcedure WITH myVar, myVar2

By default, variables and arrays pass to procedures by reference. Therefore, changes made in the procedure to passed variables and arrays are passed back to the calling program. For example, suppose the procedure increments the value in myVar by the value in myVar2. The modified value of myVar becomes the new value of myVar when the procedure returns control to the calling program.

Alternatively, if you want to use the DO command but want to pass data by value, enclose each parameter with parentheses (()) as shown in the following example:

DO myProcedure WITH (myVar), (myVar2)

When you call a function, you can pass data using a parameter list enclosed in parentheses (()) as shown in the following line of code:

myFunction(myVar, myVar2)

By default, variables and arrays pass to user-defined functions by value. Therefore, changes made in the function to passed variables and arrays are not passed back to the calling program. However, you can pass variables and arrays by reference by prefacing variables and arrays with the at sign (@) as shown in the following line of code:

myFunction(@var1, @var2, ...)

The following table summarizes the ways you can pass variables to procedures and functions in the example.

Procedure or function call Comments

DO myProcedure WITH var1, var2, ...

Calls a procedure and passes variables by reference.

DO myProcedure WITH (var1), (var2), ...

Calls a procedure and passes variables by value.

myFunction(var1, var2, ...)

Calls a function and passes variables by value.

myFunction(@var1, @var2, ...)

Calls a function and passes variables by reference.

See Also

Show: