測了一下, 發現是 Variable 的問題
所以記錄一下
<%
function BadFunction(a)
Dim a1
a1 = a
a1 = a1 + 1
BadFunction = a
end function
function GoodFunction(a)
Dim a1
a1 = a + 1
GoodFunction = a1
end function
Variable_Bad = 2
Variable_Good = 2
response.write "Variable_Bad : " & Variable_Bad & "<br>"
response.write "BadFunction : " & BadFunction(Variable_Bad) & "<br>"
response.write "Variable_Bad : " & Variable_Bad & "<br>"
response.write "Variable_Good : " & Variable_Good & "<br>"
response.write "GoodFunction : " & GoodFunction(Variable_Good) & "<br>"
response.write "Variable_Good : " & Variable_Good & "<br>"
%>
function BadFunction(a)
Dim a1
a1 = a
a1 = a1 + 1
BadFunction = a
end function
function GoodFunction(a)
Dim a1
a1 = a + 1
GoodFunction = a1
end function
Variable_Bad = 2
Variable_Good = 2
response.write "Variable_Bad : " & Variable_Bad & "<br>"
response.write "BadFunction : " & BadFunction(Variable_Bad) & "<br>"
response.write "Variable_Bad : " & Variable_Bad & "<br>"
response.write "Variable_Good : " & Variable_Good & "<br>"
response.write "GoodFunction : " & GoodFunction(Variable_Good) & "<br>"
response.write "Variable_Good : " & Variable_Good & "<br>"
%>
輸出如下:
Variable_Bad : 2
BadFunction : 3
Variable_Bad : 3 ---> function 外的變數也被改變了
Variable_Good : 2
GoodFunction : 3
Variable_Good : 2
BadFunction : 3
Variable_Bad : 3 ---> function 外的變數也被改變了
Variable_Good : 2
GoodFunction : 3
Variable_Good : 2
依結果看來
變數似乎是傳址 (Call by address), 而非傳值 (Call by value)
以致於如果 function 內直接對 input 變數做任何異動時
function 外的變數也會隨之改變
所以最好宣告另一個變數, 儲存 input 變數處理後的結果
不要直接處理 input 變數...
沒有留言:
張貼留言