You are on page 1of 5

Modular Programming: Sub Procedures

A procedure is a series of statements that are grouped together to complete a task.


There are two types of procedures we will discuss in this class:
(1) Sub (short for subroutine) procedures
(2) Function procedures.
Reasons for making programs modular:
- Your code will be more organized and it will be easier for others to understand your code. Textbook authors
organize material into chapters for the same reason.
- Modular programming allows multiple people to work on the same program at the same time in parallel. Each
person can develop a different procedure which will be combined together in a larger program later on.
- It is easier to find mistakes and errors in a program that separates tasks into different procedures.
- In many programs you will need to execute the exact same set of commands multiple times. With modular
programming, you only need to write those set of commands once in a single procedure. This can shorten your code
significantly.
- If you write all your programs in a modular fashion, it will be easy to later reuse procedures in different programs.
Sub Procedures:
The general syntax for a Sub procedure is the following:
Sub subname ( arguments* )
statements*
Exit Sub*
statements*
End Sub
Note: The items in * are often used, but optional.
Sub procedures are invoked (or called) by the Call statement.
Call subname(arguments)
If a Sub procedure is called in the middle of another program, that program is halted until the Sub procedure is
completed.

Example: Using Sub procedures to perform certain tasks


In the following example, there are numbers in cells B2 and B3. We will develop a macro that calculates the
difference of these two numbers and displays the result in B5.

Sub math()
Dim num1 As Double, num2 As Double
Dim diff As Double, prod As Double
Sheets("Sheet1").Select
Range("B2").Select: num1 = ActiveCell.Value
Range("B3").Select: num2 = ActiveCell.Value
diff = num1 - num2
prod = num1 * num2
Range("B5").Select: ActiveCell.Value = diff
Range("B6").Select: ActiveCell.Value = prod
End Sub
Create a button to run the macro.

We will now change the math Sub procedure so that obtaining the values from the worksheet and the
subtraction/multiplication will be handled by separate Sub procedures.
Option Explicit
Sub math()
Dim num1 As Double, num2 As Double
Dim diff As Double, prod As Double
Call getvals(num1, num2)
Call calcdp(num1, num2, diff, prod)
MsgBox num1 & " " & num2 & " " & diff & " " & prod
Range("B5").Select: ActiveCell.Value = diff
Range("B6").Select: ActiveCell.Value = prod
End Sub
Sub calcdp(num1, num2, diff, prod)
diff = num1 - num2
prod = num1 * num2
End Sub
Sub getvals(num1, num2)
Sheets("Sheet1").Select
Range("B2").Select: num1 = ActiveCell.Value
Range("B3").Select: num2 = ActiveCell.Value
End Sub
No variables in calcdp or getvals need to be declared because they are all in argument list.
In the argument list, the first arguments are linked, second arguments are linked, etc.

Example: Reusing Sub procedures, declare vars in Sub procedures


Option Explicit
Sub main()
Dim a As Double, b As Double, x As Double, y As Double
a = 1: b = 2: x = 5: y = 6
MsgBox "a = " & a & ", b = " & b & ", x = " & x & ", y = " & y
Call addy(a, b, sumab)
MsgBox "a = " & a & ", b = " & b & ", x = " & x & ", y = " & y
Call addy(x, y, sumxy)
MsgBox "a = " & a & ", b = " & b & ", x = " & x & ", y = " & y
MsgBox sumab & " " & sumxy
End Sub
Sub addy(m, n, answer)
Dim answer As Double
answer = m + n
MsgBox "The sum of " & m & "+" & n & "=" & answer
m = 900
End Sub

Variable Scope:
By default, variables are locally defined.
Example: [ACT IT OUT IN CLASS]
In this example, we take another look at how arguments are passed to Sub procedures.
Try running this code:
Option Explicit
Sub main()
Dim a As Double, b As Double, x As Double, y As Double
a = 1: b = 2: x = 5: y = 6
MsgBox "Before subby: " & a & " " & b & " " & x & " " & y
Call subby(a, b, x, y)
MsgBox "After subby: " & a & " " & b & " " & x & " " & y
End Sub
Sub subby(n, m, a, b)
Dim x As Double, y As Double
MsgBox "Start of subby: " & a & " " & b & " " & x & " " & y
n = 15: m = 40: a = 100: b = 200: x = 0.1: y = 0.2
MsgBox "End of subby: " & a & " " & b & " " & x & " " & y
End Sub

This example shows that variables have local scope; Variables are affected only by commands within their own
procedure. A variable called x in main( ) is distinct from a variable called x in subby( ). If x is changed in
subby( ), it does not affect the variable x in main( ).
It is possible to extend the scope of a variable to all procedures in a module or to an entire project (see Chapter
8), but it is not recommended and we will not be learning about it in this class.

Passing By Value vs Passing By Reference:


Place parentheses around arguments to protect variables from being overwritten. The parentheses are like a
shield of protection.
Option Explicit
Sub main()
Dim a As Double, b As Double, c As Double, d As Double
a = 1: b = 2: c = 3: d = 4
MsgBox "Before changeval: " & a & " " & b & " " & c & " " & d
Call changeval((a), b, (c), d)
MsgBox "After changeval: " & a & " " & b & " " & c & " " & d
End Sub
Sub changeval(w, x, y, z)
MsgBox "In changeval: " & w & " " & x & " " & y & " " & z
w = 50: x = 55: y = 60: z = 65
End Sub

You might also like