ByREF
ByRef
is a modifier used in function parameters to specify that the parameters is passed by reference (that is,
the original variable is passed to the FUNCTION or SUB).
Syntax
It's used in function parameter declaration as in this example:
Here the variable a
is being modified within the function, and this modification persist upon return.
Except for arrays, when ByRef
or ByVAL is not specified in FUNCTION or SUB
parameters, ByVAL will be used by default. On the other hand, if the parameter is an array,
and no access is specified, it's supposed to be ByRef
(arrays cannot be passed by value).
ByRef allows us to pass arrays to FUNCTION or SUB:
When passing arrays like in this example, if ByRef
can be omitted.
Arrays parameters cannot be passed to a function using ByVAL.
ByRef
is also useful to return values in the parameters. RETURN allows to return a single value
from a FUNCTION, but we can return several values by storing the result in those parameters.
ByRef
requires the parameter to be an lvalue, that is, a variable, an array or an array cell. You cannot use
ByRef
with expressions (i.e. numbers) because you cannot store a value in them.
Example of wrong usage:
See also: