Parameters passed by value and by reference

[<<<] [>>>]

When you call a function or subroutine and pass variables as arguments do you pass the value of the variable or the variable itself. Look at the following simple example:

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

Will it print 1 or 2? The answer is that it will print 2. ScriptBasic passes the variables and not the value of the variable whenever possible. If we alter the program a bit and write

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

the resulting output will be 1. Here the difference is the use of the operator ByVal. This operator is an identity operator that does nothing, but this doing nothing is valuable. The result of this "nothing" is that the passed value becomes an expression and is not a variable anymore. As such can not be passed by reference only by value the function can not alter the value of the variable a.


[<<<] [>>>]