If you use an ordinary variable (ie, not an expression) as the value when calling a subroutine or a function, the argument within the subroutine/function will point back to the variable used in the call and any changes to the argument in your routine will also be made to the supplied variable. This is called passing arguments by reference.
For example, you might define a subroutine to swap two values, as follows:
1 2 3 4 5 6 |
Sub Swap a, b Local t t = a a = b b = t End Sub |
In your calling program you would use variables for both arguments:
1 |
Swap nbr1, nbr2 |
And the result will be that the values of nbr1 and nbr2 will be swapped.
Unless you need to return a value via the argument you should not use an argument as a general purpose variable inside a subroutine or function. This is because another user of your routine may unwittingly use a variable in their call and that variable will be “magically” changed by your routine. It is much safer to assign the argument to a local variable and manipulate that instead.
Leave a Reply