VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

Variable Scope in Procedures

This section describes rules on scope of global variables defined in the main code and local variables defined in procedures.

Variable Scope - The area of source code where a variable is accessible.

If you are not using procedures, variable scope is very simple. The scope of a variable is: from the statement where it is defined to the last statement of the code.

If you are using procedures, variable scope gets more complex. Here are some basic rules:

1. Global - If a variable is defined in the main code, its scope is from the statement where it is defined to the last statement of the entire code including all procedures.

2. Local - If a variable is defined in a procedure code, its scope is from the statement where it is defined to the last statement of the procedure.

3. Collision - If a variable is explicitly defined in a procedure code has the same name as a variable defined in the main code, the variable of the main code become in-accessible within this procedure.

There are some interesting consequences of those rules:

  • The nice thing about rule #1 is that variables defined the main code are automatically accessible in all procedures. You don't have to pass them as reference arguments to share them in a procedure.
  • The bad thing about rule #2 is that if you are using temporary variable in a procedure without explicit declaration, you could accidentally change the value of a global variable of the same name.
  • Rule #3 helps us to avoid the bad impact of rule #3, if you declare all temporary variables explicitly in procedures.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

"Function" and "Sub" Procedures

 What Is a Procedure?

 "Function" Statement and Function Call

 Function Procedure Example

 "Sub" Statement and Subroutine Call

 Sub (Subroutine) Procedure Example

 Passing Arguments to Procedures

 Example - Passing Arguments by Reference

 Example - Passing Arguments by Value

 Passing Arrays as Arguments

Variable Scope in Procedures

 Example - Variable Scope in Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

Variable Scope in Procedures - Updated in 2015, by Dr. Herong Yang