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 www.wrox.com.)

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

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.