Chapter 4. Variables and Procedures

In this chapter, the discussion of VBScript variables continues and also expands to include VBScript procedures and functions. Some important variable-specific topics have not been discussed yet, including rules for naming and declaring variables, the important Option Explicit statement, and the concepts of variable scope and lifetime. You also learn the syntax for defining procedures and functions, including arguments and return values, and get introduced to some "design strategies" for your scripts.

If you are already an experienced programmer in another language and tempted to skip this chapter, you may try just skimming it instead. Even where the material is rudimentary programming-wise you will pick up some useful information that is particular to VBScript.

Option Explicit

You might not be able to guess it based on the code examples presented so far, but declaring variables in VBScript is optional. That's right. You can just start using a new variable anywhere in your code without having declared it first. There is no absolute requirement that says that you must declare the variable first. As soon as VBScript encounters a new nondeclared variable in your code, it just allocates memory for it and keeps going. Here's an example. (The script file for this code is OPTION_EXPL_NO_DECLARE.VBS; you can download the code examples for each chapter in this book from

lngFirst = 1 lngSecond = 2 lngThird = lngFirst + lngSecond MsgBox lngThird ...

Get VBScript Programmer's Reference, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.