VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.01

Procedures - Functions and Subroutines

Part:   1  2  3  4  5  6  

VB Script Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Data Types and Literals

Variables

Logic Operations

String Operations

Conditional Statements

Arrays

Loop Statements

Functions and Subroutines

Built-in Functions

Variable Inspection

... Table of Contents

(Continued from previous part...)

Variable Scope 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.

Example - Variable Scopes

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) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<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:

  • There are 6 variables in this example: vGlobalDim, vGlobalNoDim, vLocalDim, vLocalNoDim, vTempDim, and vTempNoDim.
  • The behavior of vGlobalDim and vGlobalNoDim is pretty consistent, defined in the main code; and accessible in the procedure. "Dim" or not makes no difference.
  • The behavior of vLocalDim and vLocalNoDim is also consistent, define in the procedure, not accessible in the main code. Notice that vLocalDim and vLocalNoDim are empty in the "after Sub" message.
  • The behavior of vTempDim and vTempNoDim shows that "Dim" statement forces vTempDim to a new local variable. So we have two variables of the same name, one in the main code, and one in the procedure. This is why vTempDim still has the old value after the subroutine call.

Conclusions

  • Be careful about the syntax of calling subroutine without the keyword "Call". You can not put argument list inside parentheses.
  • Function procedure provides the return value through the function name.
  • "Exit Function/Sub" can be used to terminate a procedure.
  • "ByRef" keyword is optional. It shares the data with the calling code.
  • "ByVal" keyword is required. It makes a copy of the data received from the calling code.
  • Variable defined in the main code is globally accessible in any procedure.
  • Variable defined in a procedure is only accessible in that procedure.
  • Recursive calling of a procedure is allowed.
  • I don't know how to specify an array as the return value of a function procedure.

Part:   1  2  3  4  5  6  

Dr. Herong Yang, updated in 2006
VBScript Tutorials - Herong's Tutorial Notes - Procedures - Functions and Subroutines