SET UDFPARMS Command

Specifies if Microsoft Visual FoxPro passes parameters to a user-defined function (UDF) by value or by reference.

SET UDFPARMS TO VALUE | REFERENCE

Parameters

  • TO VALUE
    Specifies that a variable be passed to a user-defined function by value. When a variable is passed by value, the variable's value can be changed in the user-defined function, but the variable's original value in the calling program isn't changed.
  • TO REFERENCE
    Specifies that a variable be passed to a user-defined function by reference. When a variable is passed by reference and the user-defined function changes the value of the passed variable, the variable's original value in the calling program is also changed.

Remarks

By default, variables are passed to a user-defined function by value. (Variables passed to procedures with DO ... WITH are passed by reference.)

You can force parameters to be passed to a UDF by value or reference, regardless of the setting of SET UDFPARMS. Enclose a variable in parentheses to force the variable to be passed by value. Preface the variable with an @ symbol to force the variable to be passed by reference.

Tip   Entire arrays can be passed to a procedure or user-defined function. The entire array is passed if you issue SET UDFPARMS TO REFERENCE or preface the array name with @. The first element of the array is passed by value if you issue SET UDFPARMS TO VALUE or enclose the array name by parentheses. Array elements are always passed by value.

Example

The following example illustrates the difference between passing variables by value and by reference.

** Pass variable by value ** CLEAR SET TALK OFF WAIT 'Press a key to pass by value' WINDOW SET UDFPARMS TO VALUE STORE 1 TO gnX

** The value of gnX is unchanged ** @ 2,2 SAY 'UDF value: ' + STR(plusone(gnX)) @ 4,2 SAY 'Value of gnX: ' + STR(gnX)

** Pass variable by reference ** WAIT 'Press a key to pass by reference' WINDOW CLEAR SET UDFPARMS TO REFERENCE STORE 1 TO gnX ** The value of gnX is changed ** @ 2,2 SAY 'UDF value: ' + STR(plusone(gnX)) @ 4,2 SAY 'Value of X: ' + STR(gnX) SET UDFPARMS TO VALUE

** This is a UDF that adds one to a number ** FUNCTION plusone PARAMETER gnZ gnZ = gnZ + 1 RETURN gnZ ** End of UDF **

The following is the above example with variables passed by value and reference through the use of parentheses and @, respectively.

** Pass variable by value ** CLEAR SET TALK OFF WAIT 'Press a key to pass by value' WINDOW STORE 1 TO gnX @ 2,2 SAY 'UDF value: ' + STR(plusone((gnX))) @ 4,2 SAY 'Value of gnX: ' + STR(gnX)

** Pass variable by reference ** WAIT 'Press a key to pass by reference' WINDOW CLEAR STORE 1 TO gnX @ 2,2 SAY 'UDF value: ' + STR(plusone(@gnX)) @ 4,2 SAY 'Value of gnX: ' + STR(gnX)

** This is a UDF that adds one to a number ** FUNCTION plusone PARAMETER gnZ gnZ = gnZ + 1 RETURN gnZ ** End of UDF **

See Also

DO | LPARAMETERS | PARAMETERS | PARAMETERS( ) | PROCEDURE