ByVal command

[<<<] [>>>]

As you could see the caller can alter the behavior of argument passing forcing the argument to be passed by value instead of reference. This can also be done inside the called function or subroutine. To do this the pair of the ByVal operator, the ByVal command, can be used. The syntax of the command is very simple:

ByVal variable list

The variables listed in the command become all passed by value instead of passed by reference and can be altered without modifying the caller variable. For example

a = 1 call MySub(a) print a sub MySub(x) ByVal x x = x + 1 end sub

Will print 1, because the local variable x gets the value of a and not the reference to a. Note however that this is a command executed by ScriptBasic and is not a declaration. Therefore

a = 1
call MySub(a)
print a
sub MySub(x)
x = x + 1
ByVal x
X = x + 1
end sub

will print 2. Why? Because the first increment is done on the reference to a, and therefore a is incremented. After this increment the command ByVal is executed. This command evaluates the variables listed after it and assigns the values to the variables. The only effect is that the variables now contain the value copied from the originally referenced variable and not a reference to the caller variable.


[<<<] [>>>]