c-- styles for logos and headline links do not modify internet, red, or black styles -->
|
|
|
|
|
|
|
|
By Paul Lomax, Matt Childs & Ron
Petrusha
Passing Variables into a SubroutineThe ability to pass variables from one procedure to another is an important part of using custom procedures. It allows us to write custom "black box" routines that can behave differently depending on where the routine has been called from and also on the particular data values that the routine receives from the calling program. The data is passed from a calling routine to a subroutine by an argument list. The argument list is delimited with commas and can contain any data subtypes, including objects and arrays. For instance, the following mySubRoutine procedure expects three arguments: intDataIn1, strDataIn2, and lngDataIn3:
When mySubRoutine is called from AnotherSubRoutine, it is passed three variables as arguments: intvar1, strvar2, and lngvar3. So as you can see, the names of variables passed in the calling routine's argument list do not need to match the names in the custom procedure's argument list. However, the number of variables in the two argument lists does need to match or a runtime error results. In addition, because VBScript is so flexible in its use of data types, you must take care when building subroutines that use data passed into them. The variables designated in the custom subroutine's argument list are automatically assigned the data types of the calling program's argument list. If a custom subroutine attempts to perform some inappropriate operation on the data passed to it, an error results, as the following code fragment illustrates:
The custom subroutine mySubRoutine assumed that intDataIn1 would be an integer, but instead the calling program passed it a string variable, intVar1. Therefore VBScript automatically cast intData1 as a string. The subroutine then produces a runtime error when it attempts to perform multiplication on a non-numeric variable. As you can see, while loosely typed languages like VBScript have many advantages, one of their major drawbacks is the fact that you must be on your guard for rogue data at all times. Note that you can pass an argument to a procedure either by reference or by value. By default, arguments are passed by reference. By reference means that the calling routine passes the called function or subroutine a pointer to the argument (that is, its actual address in memory). As a result, any modifications made to the variable are reflected once control returns to the calling routine. The ASP code in Example 2-3 illustrates passing a variable by reference. The variable x is initially assigned a value of 10 in the DoSubroutine procedure. This value is then changed to 100 in the CallAnotherSub procedure. When control returns to the DoSubroutine procedure, the value of x remains 100 because the variable was passed by reference to CallAnotherSub. Example 2-3: Passing a Variable by Reference
Note that the
is identical to:
On the other hand, by value means that the calling routine passes the called function or subroutine a copy of the variable. This means that any changes to the variable's value are lost when control returns to the calling program. The ASP code in Example 2-4 illustrates passing a variable by value. As was also true in Example 2-3, the variable x is initially assigned a value of 10 in the DoSubroutine procedure. This value is then changed to 100 in the CallAnotherSub procedure. When control returns to the DoSubroutine procedure, the value of x reverts back to 10 because the variable x was passed by value to CallAnotherSub. Example 2-4: Passing a variable by value
Note that the There is one additional way to pass an argument by value to a
procedure that involves overriding the explicit or default
x would have been passed by value to the routine rather than by reference. The parentheses, in other words, cause an argument to be passed by value to a routine that is expecting to receive a parameter by reference. (Note that the converse does not work: parentheses do not cause an argument to be passed to reference to a routine that is expecting to receive a parameter by value.) This is a subtle difference that you should be aware of when passing parameters to procedures, since it can have unintended consequences. |
|