Function chk(ByVal x As String) As Long
Dim i As Long
For i = 1 To Len(x)
chk = chk + Mid(x, i, 1)
Next
If chk > 9 Then chk = chk(chk)
End Function
I wondered that the compiler did not find an error in the expression...
chk = chk + CLng(Mid(x, i, 1))
or by implicit conversion in numeric operation:
chk = chk - Mid(x, i, 1)
Next
chk = -chk
If the ByVal keyword is omitted:
Function chk(x As String) As Long 'causes compiler error
...then the compiler raises an error "ByRef argument type mismatch". But if the argument type is also omitted then there is no error.
Function chk(x)
Dim i
For i = 1 To Len(x)
chk = chk + CLng(Mid(x, i, 1))
Next
If chk > 9 Then chk = chk(chk)
End Function
Another purpose of this article is to show once again that a function name can be used as a normal variable within the function code; i.e., there is no need to declare an auxiliary variable as some programmers do:
Function chk(x)
Dim i, aux 'aux is actually redundant'
For i = 1 To Len(x)
aux = aux + CLng(Mid(x, i, 1))
Next
chk = aux
If chk > 9 Then chk = chk(chk)
End Function
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)