Example - Variable Scope in Procedures

This section provides a tutorial example on validating scope of global variables in the main code and local variables in procedures.

To help you understand variable scope concepts, I wrote the following sample code, function_variable_scope.html:

<html>
<body>
<!-- function_variable_scope.html
 - Copyright (c) 1998 HerongYang.com. All Rights Reserved.
-->
<pre>
<script language="vbscript">
   Dim vGlobalDim
   vGlobalDim = "Cat"
   vGlobalNoDim = "Dog"

   Dim vTempDim
   vTempDim = "Bird"
   vTempNoDim = "Fish"

   Call ScopeCheck()

   document.writeln("")
   document.writeln("Current value after Sub:")
   document.writeln("   vGlobalDim = " & vGlobalDim)
   document.writeln("   vGlobalNoDim = " & vGlobalNoDim)
   document.writeln("   vLocalDim = " & vLocalDim)
   document.writeln("   vLocalNoDim = " & vLocalNoDim)
   document.writeln("   vTempDim = " & vTempDim)
   document.writeln("   vTempNoDim = " & vTempNoDim)

Sub ScopeCheck()
   Dim vLocalDim
   vLocalDim = "Apple"
   vLocalNoDim = "Orange"

   Dim vTempDim
   vTempDim = "Banana"
   vTempNoDim = "Grape"
   
'  Updating values
   vGlobalDim = vGlobalDim & " - Updated by Sub"
   vGlobalNoDim = vGlobalNoDim & " - Updated by Sub"
   vLocalDim = vLocalDim & " - Updated by Sub"
   vLocalNoDim = vLocalNoDim & " - Updated by Sub"
   vTempDim = vTempDim & " - Updated by Sub"
   vTempNoDim = vTempNoDim & " - Updated by Sub"

'  Showing values
   document.writeln("")
   document.writeln("Current value in Sub:")
   document.writeln("   vGlobalDim = " & vGlobalDim)
   document.writeln("   vGlobalNoDim = " & vGlobalNoDim)
   document.writeln("   vLocalDim = " & vLocalDim)
   document.writeln("   vLocalNoDim = " & vLocalNoDim)
   document.writeln("   vTempDim = " & vTempDim)
   document.writeln("   vTempNoDim = " & vTempNoDim)
End Sub
</script>
</pre>
</body>
</html>

Here is the output:


Current value in Sub:
   vGlobalDim = Cat - Updated by Sub
   vGlobalNoDim = Dog - Updated by Sub
   vLocalDim = Apple - Updated by Sub
   vLocalNoDim = Orange - Updated by Sub
   vTempDim = Banana - Updated by Sub
   vTempNoDim = Grape - Updated by Sub

Current value after Sub:
   vGlobalDim = Cat - Updated by Sub
   vGlobalNoDim = Dog - Updated by Sub
   vLocalDim = 
   vLocalNoDim = 
   vTempDim = Bird
   vTempNoDim = Grape - Updated by Sub

Here are my comments about this example:

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