The usage scope of variables in ASP
<%
Dim a
A = 10
function AA ()
Dim b
b = 5
Response.Write (a) ' output here is 10, the variables defined outside the function can be called global variables, valid at any location, including function interior, sub internal, class internal
A = 5 ' because A is defined outside AA, the change to a here will affect the value of global a
End Function
Call AA ()
Response.Write (a) ' Here the output is 5, because a is changed in the AA function.
Response.Write (b) ' Here the output is null, because B is defined within the function AA, so B is only valid within AA
%>
Look at one more example
<%
Dim a
A = 10
function AA ()
Dim a ' note that here a dim a is added to the function above, the variable declared within AA is valid only within AA, and a in this function has nothing to do with a on the outside of the function, so he does not affect the value of a outside of the function.
A = 5
End Function
Call AA
Response.Write (a) ' This output is 10, because the variable declared inside the function is only valid internally
%>
Third case
<%
Dim a
A = 10
' Note that the parameters here Use ByVal, without ByVal By default, ASP will use ByRef to pass the value, ByVal simple can be understood as a copy of the copy parameter, so the change of a in AA is actually just a copy of a change, does not affect the value of the external A, So the output a at the back is still 10.
Function AA (ByVal a)
A = 5
End Function
Call AA (a)
Response. Write (a)
%>
Fourth case
<%
Dim a
A = 10
' The way the value is passed is changed to byref, and the effect of no byref is not the same, the function in VBScript and the default method of passing values are byref
' ByRef is the actual memory address that transmits the parameter, so modifying a within a function is equivalent to modifying the value of the outer a of the function, so the following will output 5
function AA (ByRef a)
A = 5
End Function
Call AA (a)
Response. Write (a) ' The output here is 5 because the value of a in function AA is changed
%>