ASP中函数调用对参数的影响

类别:Asp 点击:0 评论:0 推荐:

在ASP编程中,经常需要自己编写一些函数(或过程)来实现某些特定的功能,这时往往需要向函数(或过程)传递相应的参数
在函数(或过程)中进行数据处理,即有可能需要保留或改变参数的值,下面有相关范例
用下面的函数(TestAddress)就可以使一个函数多个返回值成为可能(一个函数返回值,多个参数改变后的值)

范例:

<%@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 "初值:" & "&nbsp;"
 Response.Write "A1=" & A1 & "&nbsp;"
 Response.Write "B1=" & B1 & "<BR>"
 Response.Write "函数(TestValue)值:" & TestValue(A1,B1) & "<BR>"
 Response.Write "终值:" & "&nbsp;"
 Response.Write "A1=" & A1 & "&nbsp;"
 Response.Write "B1=" & B1 & "<BR><BR><BR>"

 Response.Write "初值:" & "&nbsp;"
 Response.Write "A2=" & A2 & "&nbsp;"
 Response.Write "B2=" & B2 & "<BR>"
 Response.Write "函数(TestAddress)值:" & TestAddress(A2,B2) & "<BR>"
 Response.Write "终值:" &  "&nbsp;"
 Response.Write "A2=" & A2 & "&nbsp;"
 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

%>

 

 

本文地址:http://com.8s8s.com/it/it9224.htm