ByVal versus ByRef (ASP/VBScript)
Typically, ASP developers pass parameters to VBScript FUNCTIONs and SUBs all the time. After all, creating custom functions is one of the strengths of this development environment. But did you know that the default way for passing parameters to a SUB or a FUNCTION is by reference (ByRef)?
Well, I wasn’t fully aware of that until I came across some sample code that was using ByVal to allow parameters to be passed “by value” to a function. What’s the big deal? When you pass a variable ByVal, a new instance of the variable is created and given to the routine being called (which requires more memory).
As the good folks at ASP101 observe: “Any changes made to the value of this variable have no effect on the value of the original variable that was passed in. If you instead pass in a variable ByRef, any changes you make to this variable also change it's value outside the routine.”
Consider this example function:
Function IsWhole(ByVal n)
dim i
i = cdbl(n)
IsWhole = (cdbl(round(i)) = i)
End Function
A brief discussion on the subject at ASP101.com:
ByVal Vs. ByRef

<< Home