Passing Arrays as Arguments

This section provides a tutorial example on how to pass an array as an arguments by reference to reverse positions of all elements in an array given as the argument.

As I mentioned earlier, arrays can also be passed as arguments. If an array is passed by reference, the procedure is working on the same array as the calling code. If an array is passed by value, the procedure is working on an independent copy of the original array in the calling code.

Here is an example code of using array as an argument, function_reverse_array.html:

<!-- function_reverse_array.html
 - Copyright (c) 1998 All Rights Reserved.
<script language="vbscript">
   document.writeln("Test 1: Reversing a data literal")
   bOk = ReverseArray("Apple")

   aPets = Array("Bird", "Cat", "Dog", "Fish", "Rabbit")
   document.writeln("Test 2: Reversing an array")
   document.writeln("   Before Sub: " & Join(aPets))
   bOk = ReverseArray(aPets)
   document.writeln("   After Sub: " & Join(aPets))

Function ReverseArray(ByRef aList)
   If IsArray(aList) Then
      iMin = LBound(aList)
      iMax = UBound(aList)
      For i=iMin to iMax\2
         j = iMax - (i-iMin) 
         vTemp = aList(i)
         aList(i) = aList(j)
         aList(j) = vTemp
      ReverseArray = True
      document.writeln("Error: You are not giving an array.")
      ReverseArray = False
   End If
End Function

Here is the output:

Test 1: Reversing a data literal
Error: You are not giving an array.

Test 2: Reversing an array
   Before Sub: Bird Cat Dog Fish Rabbit
   After Sub: Rabbit Fish Dog Cat Bird

My "ReverseArray" function worked perfectly. You can take it as a utility function to your application.

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)


 Full Version in PDF/EPUB