Skip to main content

Parameter Passing by Reference, continued

The example also illustrates the fact that you don't always need to pass parameters by reference. For example, if you want a procedure to return several results, you can:

  • using pass by reference: pass out several variables by reference to allow your procedure to create several variables, each holding a specific result.

  • using pass by value: write the procedure as a function that returns a delimited string or a list of the results.

Another example: if you want to change num into its double, you can use the pass by reference concept with the dblbyref1 procedure. But you can achieve the same result with the dblbyval procedure by writing set num=$$dblbyval(num).

Finally, if you want to pass an array to a procedure, you must pass it by reference.

Again, the passbyref.mac code:

passbyref ; passing parameters by value and reference
    ; pass by value
    read !, "Enter a number: ", num
    set dblnum = $$dblbyval( num )
    write !, num, " doubled is: ", dblnum

    ; num passed IN and OUT by reference
    write !, num
    do dblbyref1( .num )
    write " doubled is: ", num

    ; num passed IN by value
    ; result passed OUT by reference
    do dblbyref2(num, .result)
    write !, num, " doubled again is: ", result
    quit

dblbyval(anynum) PUBLIC
    { quit anynum * 2 }

dblbyref1(anynum) PUBLIC
    { set anynum = anynum * 2
    quit }

dblbyref2(anynum, double) PUBLIC
    { set double = anynum * 2
    quit }
FeedbackOpens in a new tab