Previous Section Table of Contents Next Section

What Are Statements and Subroutines?

Here's where VBScript's terminology gets a bit complicated, and for no good reason: Aside from the terms themselves, statements and subroutines are actually quite straightforward. A statement is an intrinsic command that accepts zero or more parameters and returns no value. A subroutine is simply a custom statement that you write yourself. Intrinsic and custom functions are both called functions; why custom statements are called subroutines (or subs for short) is a mystery from the depths of VBScript's past.

Functions, without the Output

Statements (and subroutines) always perform some kind of task. Unlike a function, statements cannot return a value to your script, so they just perform a task. One of the simplest VBScript statements is Beep, which simply makes the computer beep. It takes no parameters, returns no value, and performs one task. Another simple statement, End, tells VBScript to stop running your script immediately. Pretty simple!

graphics/arrow.gif A Custom Subroutine

You may want to create custom routines that perform a task but return no value. For example, suppose you're writing a script and you want your computer to beep a lot when it encounters some specific condition, such as an error or a full hard disk. You could just list multiple Beep statements in a series, but it would be more efficient to use a custom subroutine. You could program the subroutine to accept an input parameter describing how many times to beep. Listing 5.3 shows an example.

Listing 5.3. MultiBeep Subroutine. This subroutine can be used to make the computer beep a specified number of times.

Sub MultiBeep(iTimes)

 Dim iTemp

 For iTemp = 1 To iTimes

  Beep

 Next

End Sub

You can use the MultiBeep subroutine from anywhere in the main portion of your script. For example, to make the computer beep five times, you'd use MultiBeep(5).

graphics/arrow.gif A Custom Subroutine-Explained

The MultiBeep subroutine actually uses several VBScript commands I haven't introduced yet, but I'll focus for now on the parts that define the subroutine. First, all subroutines include a Sub and an End Sub statement, in much the same way that custom functions use Function and End Function:


Sub MultiBeep(iTimes)

End Sub

As with a custom function, note that the parameters are defined in the Sub statement. In this case, the variable iTimes represents the number of times the computer should beep.

Everything else in the subroutine is your custom code.


Dim iTemp

For iTemp = 1 To iTimes

 Beep

Next

For now, don't worry about how this code works. You probably recognize the Dim statement as a variable declaration, and you've already learned what Beep does. I'll cover the For…Next construct in Chapter 10.

TIP

Notice in Listing 5.3 that I indented the lines of script between Sub and End Sub. Indenting is a common programming practice that helps keep your code easier to read. The indent serves as a visual cue that the code is within some other routine or construct.


Leading a Dual Life

A few of VBScript's built-in functions lead a double life as statements. The most common example is MsgBox. As a statement, it displays a message box, complete with whatever icons and buttons you like. When the user clicks one of those buttons, the message box goes away and your script continues.

However, MsgBox() can be a function, too. In this guise, it still displays the same type of message box, but it also returns a value indicating which button the user clicked. For example, this allows your script to ask Are-you-sure?-type messages with Yes and No buttons and act appropriately based on which button the user clicks.

There's only one real difference in the way you use MsgBox as a statement or a function. As a statement, there's no return value, so you can use MsgBox on a line of script by itself, as in MsgBox "Hello!". However, as a function, MsgBox() returns a value, which you'll need to assign to a variable, as in iResult = MsgBox("Are you sure?").

You'll see a lot more of MsgBox, both as a function and a statement, throughout this book and especially in Chapter 6.


    Previous Section Table of Contents Next Section