[RESOLVED] A question about functions
I was just wondering how functions are returned.
If you had a function:
VB Code:
Private Function CheckSafeList(Item As String)
CheckSafeList = False
For t = 0 To UBound(GetSafeList)
If Trim(Item) = Trim(GetSafeList(t)) Then
CheckSafeList = True
Exit For
End If
Next
End Function
Would the function terminate at the 'CheckSafeList = False', or does it run through the entire function, and return the end value?
Thanks in advance =)
Re: A question about functions
In runs until the end function unless there is an early "exit" used... BTW, this seems to be better...
VB Code:
Private Function CheckSafeList(ByVal Item As String) As Boolean
CheckSafeList = False
For t = 0 To UBound(GetSafeList)
If Trim$(Item) = Trim$(GetSafeList(t)) Then
CheckSafeList = True
Exit For
End If
Next
End Function
Re: A question about functions
Also, if you want to ignore the casing of the item then you can add a UCASE$ or LCASE$ after the Trim$ functions.
VB Code:
Private Function CheckSafeList(ByVal Item As String) As Boolean
CheckSafeList = False
For t = 0 To UBound(GetSafeList)
If Trim$(UCase$(Item)) = Trim$(UCase$(GetSafeList(t))) Then
CheckSafeList = True
Exit For
End If
Next
End Function
Re: A question about functions
Ah ok, thanks for clearing that up.
Was just using that as an example, but was does the Trim$ do, is it any different from Trim?
Re: A question about functions
Nope, but it is a little bit faster, as the system doesn't have to convert into a variant. It tells it that you want to return a string, and are using a string.
Re: A question about functions
The $ sign designates a string type variable is going to be processed. Its a speed improvement but only minor unless you have a loop or something.
Re: A question about functions
Ah ok, thanks again guys =)
Re: [RESOLVED] A question about functions
Whenever you have a loop you should try and optimise it as much as possible, because loops can really slow your application down.
Removing the If() statement and moving Trim$(UCase(Item)) outside the loop can really speed it up.
VB Code:
Private Function CheckSafeList(ByRef pItem As String) As Boolean
Dim upper As Long, i As Long
upper = UBound(GetSafeList)
Do
CheckSafeList = StrComp(pItem, GetSafeList(i), vbTextCompare)
i = i + 1
Loop While (CheckSafelist And (i <= upper))
End Function
Edit: using StrComp() is even faster, then you can remove all the Trim$(Ucase()) stuff and pass the string ByRef.
Re: [RESOLVED] A question about functions
Just an interesting note:
If you had (in VB.NET)
VB Code:
Private Function Blah(ByRef n as Integer) as integer
n += 5
Return n
n -= 3
End Function
Dim x as integer = 5
Dim y as integer = 0
y = Blah(x)
'// x = 10
'// y = 10
Y would be 10 and X would be 10. X would not be 7, as would happen in VB6. In VB.Net, the function ends at a return.
VB Code:
Private Function Blah(ByRef n as Integer) as integer
n = n + 5
Blah = n
n = n - 3
End Function
Dim x as integer
Dim y as integer
x = 5
y = 0
y = Blah(x)
'// x = 7
'// y = 10
Just interesting to know.
Re: [RESOLVED] A question about functions
Yeah but in VB6 the Return keyword is used to complement GoSub. It has a completely different purpose. As has been pointed out you need to use an Exit call to quit the routine.