Tip:
Highlight text to annotate it
X
In this lesson, we will discuss the difference between passing by reference and passing by value.
In the past, when we have called Sub procedures, we have run the risk of accidentally overwriting the values of the variables in the
calling statement argument list.
In this program, a Sub procedure named changeval is called and the variables a, b, c, and d are linked with variables w, x, y, and
z in changeval.
If the values of w, x, y, or z are altered in changeval, this will alter the values of a, b, c, or d in main.
When we run this code, three message boxes appear.
The first message box states: Before changeval: 1 2 3 4
The second message box states: In changeval: 1 2 3 4
The third message box states: After changeval: 50 55 60 65
Let’s examine why these values appear.
In main, four variables named a, b, c, and d are created and given a Double data type.
a is assigned the value 1.
b is assigned the value 2.
c is assigned the value 3.
d is assigned the value 4.
A message box appears that displays the text: “Before changeval:” then the values of a, b, c, d.
The values of these four variables are 1, 2, 3, 4.
Next, the Sub procedure changeval is called.
This Sub procedure has four arguments: a, b, c, d
The variable a in main is linked to variable w in changeval.
The variable b in main is linked to variable x in changeval.
The variable c in main is linked to variable y in changeval.
The variable d in main is linked to variable z in changeval.
The variables w, x, y, and z in changeval are automatically declared and given the same data type as a, b, c, and d in main, which is
Double.
The values of w, x, y, and z in changeval are initialized to the values of a, b, c, and d in main.
w starts with the value 1.
x starts with the value 2.
y starts with the value 3.
z starts with the value 4.
Next, a message box appears that states: “In changeval:” and the values of w, x, y, and z which are 1, 2, 3, and 4.
Next, w is assigned the value 50.
x is assigned the value 55.
y is assigned the value 60.
z is assigned the value 65.
The Sub procedure changeval ends with the End Sub statement and we return to main.
The variables w, x, y, and z in changeval were all altered and this will change the value of a, b, c, and d in main.
The variable a in main has the same value as the variable w in changeval, which is 50.
The variable b in main has the same value as the variable x in changeval, which is 55.
The variable c in main has the same value as the variable y in changeval, which is 60.
The variable d in main has the same value as the variable z in changeval, which is 65.
After the Sub procedure changeval is terminated, the variables w, x, y, and z in changeval also are terminated.
The third and final message box appears and states: “After changeval:” and the values of a, b, c, d
which are now 50, 55, 60, and 65.
We can see in this example that the Sub procedure changeval alters the values of variables in the calling statement argument list.
The values of a, b, c, and d change from 1, 2, 3, 4 before the Sub procedure is called, to 50, 55, 60, and 65 after the Sub procedure is
terminated.
This occurs because we are actually passing the memory addresses of the variables a, b, c, and d in main to the variables w, x, y, and
z in changeval.
Both a in main and w in changeval refer the same location in memory.
If w is modified in changeval, this is equivalent to modifying a in main since you are modifying the same location in memory.
Similarly, both b in main and x in changeval refer to the same location in memory.
c in main and y in changeval refer to the same location in memory.
d in main and z in changeval refer to the same location in memory.
This process is called “passing by reference.”
Sometimes you want to pass only the values of the arguments to the Sub procedure and not allow the arguments to be updated after the Sub
procedure is terminated.
This process is called “passing by value” and can be accomplished by placing parentheses around the arguments in the calling statement
argument list.
Place parentheses around a and c and rerun the code.
Again, three message boxes appear.
The first message box states: Before changeval: 1 2 3 4
The second message box states: In changeval: 1 2 3 4
The third message box states: After changeval: 1 55 3 65
Let’s see what this occurs.
The first message box will state: “Before changeval:” and the values of a, b, c, d.
These variables are initially assigned the values 1, 2, 3, 4.
Now we call changeval.
Only the values of a and c in main are passed to the variables w and y in changeval.
If the values of w and y in changeval are updated, this will not affect the values of a and c in main.
However, if the values of x and z in changeval are updated inside changeval, this will affect the values of b and d in main because there are
no parentheses around them.
Passing by value allows information to flow in one direction only – from main to changeval – but not back up to main when changeval
ends.
Inside changeval, the variables w, x, y, and z are created and initialized to the values of a, b, c, and d in main, which are 1, 2, 3, and 4.
The second message box will state: “In changeval:” and values of w, x, y, and z which are 1, 2, 3, and 4.
Next, the variables w, x, y, and z are assigned new values: 50, 55, 60, and 65.
The Sub procedure changeval is terminated with the End Sub statement.
Now we return to main.
Although w and y have been updated in changeval, these changes will not be passed back up to a and c in main.
a and c in main will retain the values of 1 and 3.
x and z in changeval have been updated to 55 and 65 and these changes will be passed up to b and d in main.
The third message box states: “After changeval:” and the values of a, b, c, and d in main
which are 1, 55, 3, 65.
In summary, if you do not place parentheses around an argument in the calling statement argument list, you are “passing by reference.”
When you pass by reference, you allow the Sub procedure to modify the arguments.
If you place parentheses around an argument in the calling statement argument list, you are “passing by value.”
When you pass by value, you protect the argument from being modified when the Sub procedure is terminated.