Previous Page
Next Page

Working with Subroutines

In this section, you'll learn about how network administrators use subroutines. For many readers, the use of subroutines will be somewhat new territory and might even seem unnecessary, particularly when you can cut and paste sections of working code. But before we get into the how-to, let's go over the what.

A subroutine is a named section of code that gets run only when something in the script calls it by name. Nearly every script we've worked with thus far has been a group of commands, which have been processed from top to bottom in a consecutive fashion. Although this consecutive processing approach, which I call linear scripting, makes the code easy for the network administrator to work with, it does not always make his work very efficient. In addition, when you need to perform a similar activity from different parts of the script, using the inline cut-and-paste scripting approach quickly becomes inefficient and hard to understand. This is where subroutines come into play. A subroutine is not executed when its body is defined in the code; instead, it is executed only when it is called by name. If you define a subroutine, and use it only one time, you might make your script easier to read or easier to maintain, but you will not make the script shorter. If, however, you have something you want to do over and over, the subroutine does make the script shorter. The following summarizes uses for a subroutine in Microsoft Visual Basic, Scripting Edition (VBScript):

  • Prevents needless duplication of code

  • Makes code portable and reusable

  • Makes code easier to troubleshoot and debug

  • Makes code easier to read and maintain

  • Makes code easier to modify

The following script (LinearScript.vbs) illustrates the problem with linear scripting. In this script are three variables: a, b, and c. Each of these is assigned a value, and you need to determine equality. The script uses a series of If Then...Else statements to perform the evaluative work. As you can see, the code gets a little redundant by repeating the same statements several times.

LinearScript.vbs

Option Explicit
Dim a
Dim b
Dim c

a=1
b=2
c=3

If a = b Then
WScript.Echo a & " and " & b & " are equal"
Else
WScript.Echo a & " and " & b & " are not equal"
End If

If b = c Then
WScript.Echo b & " and " & c & " are equal"
Else
WScript.Echo b & " and " & c & " are not equal"
End If

If a + b = c Then
WScript.Echo a+b & " and " & c & " are equal"
Else
WScript.Echo a+b & " and " & c & " are not equal"
End If

OK, so the script might be a little redundant, although if you're paid to write code by the line, this is a great script! Unfortunately, most network administrators are not paid by the line for the scripts they write. This being the case, clearly you need to come up with a better way to write code. (I am telegraphing the solution to you now...) That's right! You will use a subroutine to perform the evaluation. The modified script uses a subroutine to perform the evaluation of the two numbers. This results in saving two lines of code for each evaluation performed. In this example, however, the power is not in saving a few lines of codeit's in the fact that you use one section of code to perform the evaluation. Using one section makes the script easier to read and easier to write.

Note

Business rules is a concept that comes up frequently in programming books. The idea is that many programs have concepts that are not technical requirements but still must be adhered to. These are nontechnical rules. For instance, a business rule might say that when a payment is not received within 30 days after the invoice is mailed, a follow-up notice must be sent out, and a 1 percent surcharge is added to the invoice amount. Because businesses sometimes change these nontechnical requirements, such rules would be better incorporated into a separate section of the code (a subroutine, for example) as opposed to sprinkling them throughout the entire program. If the business later decides to charge an additional 1 percent surcharge after 60 days, this requirement can be easily accommodated in the code.


In the script you are currently examining, your business rules are contained in a single code section, so you can easily modify the code to incorporate new ways of comparing the three numbers (to determine, for example, that they are not equal instead of equal). If conditions are likely to change or additional information might be required, creating a subroutine makes sense.

Quick Check

Q.

To promote code reuse within a script, where is one place you can position the code?

A.

You can place the code within a subroutine.

Q.

To make changing business rules easier to update, where is a good place to position the rules?

A.

You can place business rules within a subroutine to make them easier to update.


Calling the Subroutine

In the next script you'll examine, SubRoutineScript.vbs, the comparison of a, b, and c is done by using a subroutine called Compare. To use a subroutine, you simply place its name on a line by itself. Notice that you don't need to declare the name of the subroutine because it isn't a variable. So, the script works even though you specified Option Explicit and did not declare the name used for the subroutine. In fact, you cannot declare the name of your subroutine. If you do, you will get a "name redefined" error.

Creating the Subroutine

Once you decide to use a subroutine, the code for creating it is very light. Indeed, all that is required is the word Sub followed by the name you will assign to the subroutine. In the SubRoutineScript.vbs script, the subroutine is assigned the name Compare by the following line: Sub Compare. That's all there is to it. You then write the code that performs the comparison and end the subroutine with the command End Sub. After you do all that, you have your subroutine.

SubRoutineScript.vbs

Option Explicit
Dim a, b, c
Dim num1, num2
a=1
b=2
c=3

num1 = a
num2 = b
compare

num1 = b
num2 = c
compare

num1 = a + b
num2 = c
compare

Sub Compare
If num1 = num2 Then
WScript.Echo (num1 & " and " & num2 & " are equal")
Else
WScript.Echo(num1 & " and " & num2 & " are not equal")
End If
End Sub

Just the Steps

To create a subroutine

1.
Begin the line of code with the word Sub followed by name of the subroutine.

2.
Write the code that the subroutine will perform.

3.
End the subroutine by using the End Sub command on a line by itself.



Previous Page
Next Page