Tip:
Highlight text to annotate it
X
In this example, we will call the same Sub procedure multiple times in a program.
One of the great benefits of modular programming is that it allows you to reuse procedures as many times as you like.
Let’s say we want to make a simple Sub procedure that takes two arguments as input and tells the user the sum of those two
arguments.
Here we see that the Sub procedure addy is called twice within the macro main.
When we run the code we create five message boxes.
a = 1, b = 2, x = 5, y = 6
The sum of 1+2=3
a = 900, b = 2, x = 5, y = 6
The sum of 5+6=11
a = 900, b = 2, x = 900, y = 6
Let’s examine why these message boxes appear.
We create a Sub procedure named main and declare four variables: a, b, x, y
These variable are given a Double data type.
Next, these variables are assigned values.
a is assigned the value 1, b is assigned the value 2, x is assigned the value 5,
y is assigned the value 6.
A message box appears that states:
“a =” the value of a “, b =” the value of b “, x =” the value of x
“, y =” the value of y
At this location in the program, a has the value 1, b has the value 2,
x has the value 5, y has the value 6.
Next the program calls the Sub procedure addy, which has two arguments.
The current Sub procedure we are in, which is named main, halts until addy is completed.
Notice that the two arguments in the calling statement are named a and b, but the arguments down below where addy is defined
have the names m and n.
The first argument in the calling statement list, a, becomes linked to first argument in addy, which is named m.
If m changes in addy, a will be affected in main.
Similarly, the second argument in the calling statement list, b, becomes linked to n in addy.
If n changes in addy, b will be affected in main.
When the Sub procedure addy begins, m starts with the value of a in main , which is 1.
n starts with the value of b in main, which is 2.
Notice that we do not need to declare the variables m and n in addy because they are in the argument list.
All variables in the argument list are automatically declared when the Sub procedure begins.
m and n are given the same data types as a and b in main, which is Double.
We do need to declare variables that are not in the argument list, such as answer.
answer is declared as a Double variable and assigned the sum of m and n, which is 3.
If we did not declare answer, we would receive an error message.
Next, a message box is created stating:
“The sum of” the value of m “+” the value of n “=” answer
In this case, the message box will state: The sum of 1+2=3
Finally, m is assigned the value 900.
The Sub procedure addy is terminated and we return to main.
Remember that the value of m changed to 900 in addy, which means the value of a in main also changes to 900 since the two variables are
linked together.
The variable n has not changed its value in addy, so the value of b in main will remain 2.
x and y in main were not passed down to addy so their values are still 5 and 6.
The third message box appears and states:
“a =” the value of a “, b =” the value of b “, x =” the value of x
“, y =” the value of y
So the message box will state: a = 900, b = 2, x = 5, y = 6
Next, the Sub procedure addy is called a second time and the Sub procedure main halts until addy is completed.
The first variable in the calling statement argument list, x, becomes linked to m in addy.
If m changes in addy, x will be affected in main.
The second variable in the calling statement argument list is y, and this becomes linked to n in addy.
If n changes in addy, y will be affected in main.
The variables a and b in main will not be affected because they are not in the calling statement argument list this time.
When the Sub procedure addy begins, m starts with the value of x in main, which is 5.
n starts with the value of y in main, which is 6.
As before, we do not declare m and n in addy because they are in the argument list and given the same data types as x and y in
main, which are Double.
answer is declared as a Double variable and assigned the sum of m and n, which is 11.
A fourth message box appears that states:
“The sum of” the value of m “+” the value of n “=” the value of answer
“The sum of 5+6=11” will appear on the screen.
m is then assigned the value of 900 and the Sub procedure is terminated.
Now we return to main.
The value of m in addy is linked to the value of x in main.
Since the value of m in addy was changed to 900, the value of x in main is now also 900.
The value of n in addy remained unchanged, so the value of y in main is still 6.
a and b in main were not passed down to addy so their values are still 900 and 2.
The fifth and final message box states:
“a =” the value of a “, b =” the value of b “, x =” the value of x
“, y =” the value of y
So the last message box states: a = 900, b = 2, x = 900, y = 6
This example demonstrates many important features of calling Sub procedures.
A Sub procedure may be called multiple times.
In this example we called the Sub procedure addy twice and we could have called it many more times if we wished.
Sub procedures communicate through their argument lists.
The first time addy was called, the variables a and b in main were linked to the variables m and n in addy.
The variables m and n in addy were initialized to the values of a and b in main, which were 1 and 2.
When m in addy was assigned a new value inside addy, it affected the variable a in main after addy was terminated.
Variables in the Sub procedure’s argument list are declared automatically.
We did not have to declare the variables m and n in addy.
However, variables not in the argument list need to be declared if Option Explicit is used.
The variable answer in addy was declared as a Double.
The number of arguments in the argument lists must be consistent.
There were two arguments in both the calling statement argument list and in the Sub procedure argument list.
Finally, the names of the arguments in the calling statement’s argument list can be different from the arguments’ names in the Sub
procedure argument list.
The first time addy is called, a and b are in the argument list.
The second time addy is called, x and y are in the argument list.
However, the arguments in the Sub procedure list are named m and n.