Values Before Calling the Sub:
intDefault = 1
intByRef = 1
intByVal = 1
SampleSub(intDefault, intByRef, intByVal)
Values After Calling the Sub:
intDefault = 2
intByRef = 2
intByVal = 1
This main purpose of this sample is to illustrate one of the
side effects that can occur when you pass parameters to a function
There are two ways that you can specify for VB to pass arguments to a
procedure: by reference (ByRef) or by value (ByVal).
If you specify ByRef,
then VB simply passes a pointer to the existing value in memory.
In many cases this is faster then passing the value, but
if you change the value of the parameter, the value of the underlying
variable that you passed in is also changed. This is usually not
desirable as it can make troubleshooting extremely difficult. Modifying
values in this manner is therefore almost universally frowned upon and is
generally considered poor programming practice. If you need to return a value
it's usually best to return it as the return value of a function.
The other option is to specify ByVal. This causes VB to make a
copy of the value of the variable and pass this new copy
to the procedure. The result is that any changes made to the
parameter are discarded when the routine completes execution.
It's generally slightly slower then ByRef, but in some specific situations
where execution is split between multiple computers
it can provide better performance because it
eliminates the need to pass changes to parameters back to the
As you can tell from the results of the sample above, if you
don't specify which method to use, then by default VB will pass