Procedures - Functions and Subroutines
Part:
1
2
3
4
5
6
(Continued from previous part...)
Sub Procedure Example
Here is a short example of subroutines, function_sub.html:
<html>
<body>
<!-- function_sub.html
Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
Call Hello("Tom")
Hello "Herong"
Sub Hello(sName)
document.writeln("")
document.writeln("Helo " & sName)
End Sub
</script>
</pre>
</body>
</html>
Here is the output:
Helo Tom
Helo Herong
Notice the two different ways of calling a subroutine.
Rules of Passing Arguments
As shown in previous examples, passing arguments to procedures seems to be a simple job.
But it may cause confusion if you don't following the rules. VB script has the following rules on
passing arguments to procedures:
1. By default, arguments are passed by reference. In this case, an argument name can be used
as a variable that referring (sharing) the same data as the calling code.
2. But, arguments can be passed by value, if you put the key word "ByVal" before the argument name.
In this case, an argument name can be used as a variable that contains a copy of the data provided
by the calling code.
3. Of course, you put the word "ByRef" before an argument name to declare a pass-by-reference
argument explicitly.
4. A pass-by-reference argument can be used to allow the procedure to alter data that is
associated with a variable in the calling code. This allows the procedure to output data
back to the calling code.
5. A pass-by-value argument is safer than a pass-by-reference argument,
because the procedure only gets a copy of the data. Any changes to that data will not affect
the original data in the calling code.
6. Arrays can be passed by reference.
7. Arrays can also be passed by value. In this case, the procedure will get a copy of the array.
8. I don't know how to specify an array as the return value of a function procedure.
(Continued on next part...)
Part:
1
2
3
4
5
6
|