[RESOLVED] Using function name as return value
I've never done it like this, but I do see code that does pretty often. It seems like it would be bad practice, but also that it would be faster and not cause any problems.
Example:
vb Code:
'Example #1
Private Function GenerateString(ByVal Length As Long) As String
Dim l As Long
For l = 1 To Length
GenerateString = GenerateString & Chr$(Int(Rnd * 255) + 1)
Next l
End Function
'Example #2
Private Function GenerateString(ByVal Length As Long) As String
Dim l As Long
GenerateString = Space$(Length)
For l = 1 To Length
Mid$(GenerateString, l, 1) = Chr$(Int(Rnd * 255) + 1)
Next l
End Function
vs.
vb Code:
Private Function GenerateString(ByVal Length As Long) As String
Dim l As Long, strReturn As String
For l = 1 To Length
strReturn = strReturn & Chr$(Int(Rnd * 255) + 1)
Next l
GenerateString = strReturn
End Function
Re: Using function name as return value
your third example is the way I do it... but that's related to my background and training. The first two examples have always bothered me. Not because they are prone to problems, but because it seems unnatural (and harder to read.)
-tg
Re: Using function name as return value
Why create the extra String variable when for all intents and purposes GenerateString is already a String?
Re: Using function name as return value
Quote:
Originally Posted by
MartinLiss
Why create the extra String variable when for all intents and purposes GenerateString is already a String?
That's what I wondered.
VB functions don't return to the caller until the code is finished executing, encounters an error, or encounters an Exit Function statement, so I don't really see a problem. It just seemed like bad practice but I think I might start doing it like that anyway.
Re: [RESOLVED] Using function name as return value
DigiRev, I personally have used both but tend to using the function name directly. I also do some other stuff also which may seem unnatural, borrowing techgnome's terminology.
For example, why create the variable l when you can reuse Length since it is ByVal? ;)
Code:
Private Function GenerateString(ByVal Length As Long) As String
' Dim l As Long
GenerateString = Space$(Length)
For Length = 1 To Length
Mid$(GenerateString, Length, 1) = Chr$(Int(Rnd * 255) + 1)
Next Length
End Function
Re: [RESOLVED] Using function name as return value
Quote:
Originally Posted by
LaVolpe
DigiRev, I personally have used both but tend to using the function name directly. I also do some other stuff also which may seem unnatural, borrowing techgnome's terminology.
For example, why create the variable l when you can reuse Length since it is ByVal? ;)
Code:
Private Function GenerateString(ByVal Length As Long) As String
' Dim l As Long
GenerateString = Space$(Length)
For Length = 1 To Length
Mid$(GenerateString, Length, 1) = Chr$(Int(Rnd * 255) + 1)
Next Length
End Function
Quote:
Originally Posted by
DigiRev
That's what I wondered.
VB functions don't return to the caller until the code is finished executing, encounters an error, or encounters an Exit Function statement, so I don't really see a problem. It just seemed like bad practice but I think I might start doing it like that anyway.
Wouldn't that make your code hard to understand?
Re: [RESOLVED] Using function name as return value
I personally find it hard to follow. Plus, I've also been bitten by this when an error happens... what state is your return value? I'd rather control & build/set/calc/whatever a local variable, then have one exit point where it is returned. I've never liked the VB6 method of returning values from functions anyways. But it might be based on my background.
-tg
Re: [RESOLVED] Using function name as return value
Harder, easier to follow? Proper, not proper?
Personal preferences. The key question is: "Is it correct syntax and does it work"? It does.
However, if one is part of a coding team/company, then the team/company's standards should always be adhered to. Else you are only bound by your own whims.