</SCRIPT>
=======================
结果:
a=0
sub change(byref ar)
111
sub change2(ByVal ar)
0
sub change3( ar)
333
function change4(byref ar)
444
function change5(ByVal ar)
0
function change6( ar)
666
说明vbs默认是byRef,这点和VB一样, 按地址。
范例:
<%@LANGUAGE="VBSCRIPT"%>
<%
Option Explicit
''===================================================================
'' 参数传递
'' 1.值传递参数 (Call By Value)
'' Function TestValue(ByVal A,ByVal B)
'' 函数内参数 A、B 改变 不影响 函数的外部变量
''
'' 2.指针参数 (Call By Address)
'' Function TestAddress(ByRef A,Byref B)
'' 函数内参数 A、B 改变 影响到 函数的外部变量
''
'' 说明:
'' 1. 参数可以是数字、字符、数组、对象等VBSCRIPT语言所支持的大部分类型
'' 2. 函数返回值的类型也可以是数字、字符、数组、对象等VBSCRIPT语言所支持的大部分类型
'' 3. 过程调用参数方法与函数类似
''===================================================================
Dim A1,B1
Dim A2,B2
Function TestValue(ByVal A,ByVal B)
A = A + 1
B = B + 1
TestValue = A + B
End Function
Function TestAddress(ByRef A,Byref B)
A = A + 1
B = B + 1
TestAddress = A + B
End Function
A1 = 11
B1 = 33
A2 = 11
B2 = 33
Response.Write "初值:" & " "
Response.Write "A1=" & A1 & " "
Response.Write "B1=" & B1 & "<BR>"
Response.Write "函数(TestValue)值:" & TestValue(A1,B1) & "<BR>"
Response.Write "终值:" & " "
Response.Write "A1=" & A1 & " "
Response.Write "B1=" & B1 & "<BR><BR><BR>"
Response.Write "初值:" & " "
Response.Write "A2=" & A2 & " "
Response.Write "B2=" & B2 & "<BR>"
Response.Write "函数(TestAddress)值:" & TestAddress(A2,B2) & "<BR>"
Response.Write "终值:" & " "
Response.Write "A2=" & A2 & " "
Response.Write "B2=" & B2
''======================
'' 相似过程
''======================
Sub Test_Value(ByVal A,ByVal B)
A = A + 1
B = B + 1
End Sub
Sub Test_Address(ByRef A,Byref B)
A = A + 1
B = B + 1
End Sub
'' 类似,传递数组、对象(或者在函数中改变其值、属性)
''对象直接把对象名作为参数即可
'' 数组,把数组名称作为参数
redim aryTest(2,2)
dim intNum
function Ary_Test(ByRef A)
Ary_Test = Ubound(Ary_Test,2)
end function
''调用
intNum = Ary_Test(intNum) ''值为 3
%>