![]() |
Table of Contents |
![]() |
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 OutputStatements (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!
|
Leading a Dual LifeA 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. |
![]() |
Table of Contents |
![]() |