I have an array witch I pass to a function by reference to sort it. However, seems like the array is passed byval. Can anyone solve what's the problem? (Also sort workarounds accepted)
1) The script below passes an array by-reference to the sort function.
2) Sort function outputs the sorted array values.
3) The script outputs the sorted array values. However they are not sorted.
The script outputs:
300,200,100,,
100,200,300,
'declare variables
mitta(1) = 1
mitta(2) = 2
mitta(3) = 3
sort(mitta) ' see the function below
' show variables
For i = 1 To 3
response.write mitta(i) & ","
next
' sort function
function sort(byref a)
dim num,i,j,temp
num = ubound(a)+1
For i = 0 To num - 1
For j = i + 1 To num - 1
If a(i) < a(j) Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
Next
Next
' show sorted variables
For i = 0 To num - 1
response.write a(i) & ","
a(i) = 0
next
end function
By wrapping mitta
in parentheses in the function call sort(mitta)
, you're actually passing it by value, despite the function declaration. From http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx:
The rules are
3.1) An argument list for a function call with an assignment to the returned value must be surrounded by parens: Result = MyFunc(MyArg)
3.2) An argument list for a subroutine call (or a function call with no assignment) that uses the Call keyword must be surrounded by parens: Call MySub(MyArg)
3.3) If 3.1 and 3.2 do not apply then the list must NOT be surrounded by parens.And finally there is the byref rule: arguments are passed byref when possible but if there are “extra” parens around a variable then the variable is passed byval, not byref.
Now it should be clear why the statement MySub(MyArg) is legal but MyOtherSub(MyArg1, MyArg2) is not. The first case appears to be a subroutine call with parens around the argument list, but that would violate rule 3.3. Then why is it legal? In fact it is a subroutine call with no parens around the arg list, but parens around the first argument! This passes the argument by value. The second case is a clear violation of rule 3.3, and there is no way to make it legal, so we give an error.
See also the MSDN reference for ByRef and ByVal Parameters. Instead, you should call sort
either with:
sort mitta
or:
Call sort(mitta)