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:

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

 Full Version in PDF/EPUB