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...)

Example - Passing Arguments by Reference

To see how passing arguments by reference works, I wrote the following example, function_swap_by_ref.html:

<html>
<body>
<!-- function_swap_by_ref.html
   Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
   document.writeln("")
   document.writeln("Test 1: Swapping two literals by reference")
   document.writeln("   Before Sub: " & "Apple" & " | " & "Orange")
   Call SwapByRef("Apple", "Orange")
   document.writeln("   After Sub: " & "Apple" & " | " & "Orange")

   vFirst = "Dog"
   vSecond = "Cat"
   document.writeln("")
   document.writeln("Test 2: Swapping two variables by reference")
   document.writeln("   Before Sub: " & vFirst & " | " & vSecond)
   Call SwapByRef(vFirst, vSecond)
   document.writeln("   After Sub: " & vFirst & " | " & vSecond)

Sub SwapByRef(ByRef vLeft, ByRef vRight)
   vTemp = vLeft
   vLeft = vRight
   vRight = vTemp
   document.writeln("   In Sub: " & vLeft & " | " & vRight)
End Sub
</script>
</pre>
</body>
</html>

Here is the output:


Test 1: Swapping two literals by reference
   Before Sub: Apple | Orange
   In Sub: Orange | Apple
   After Sub: Apple | Orange

Test 2: Swapping two variables by reference
   Before Sub: Dog | Cat
   In Sub: Cat | Dog
   After Sub: Cat | Dog

Here are my comments about this example:

  • Test 1 shows that data literal can be used for a "ByRef" argument. By you will not be able to receive the change done by the subroutine.
  • Test 2 shows that using variable for a "ByRef" argument lets to receive the change by the subroutine. After the subroutine call, values in vFirst and vSecond have been swapped.
  • "ByRef" keyword is optional.

Example - Passing Arguments by Value

To see how passing arguments by value works, I wrote the following example, function_swap_by_val.html:

<html>
<body>
<!-- function_swap_by_val.html
   Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
   document.writeln("")
   document.writeln("Test 1: Swapping two literals by value")
   document.writeln("   Before Sub: " & "Apple" & " | " & "Orange")
   Call SwapByVal("Apple", "Orange")
   document.writeln("   After Sub: " & "Apple" & " | " & "Orange")

   vFirst = "Dog"
   vSecond = "Cat"
   document.writeln("")
   document.writeln("Test 2: Swapping two variables by value")
   document.writeln("   Before Sub: " & vFirst & " | " & vSecond)
   Call SwapByVal(vFirst, vSecond)
   document.writeln("   After Sub: " & vFirst & " | " & vSecond)

Sub SwapByVal(ByVal vLeft, ByVal vRight)
   vTemp = vLeft
   vLeft = vRight
   vRight = vTemp
   document.writeln("   In Sub: " & vLeft & " | " & vRight)
End Sub
</script>
</pre>
</body>
</html>

(Continued on next part...)

Part:   1  2  3  4   5  6 

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