PDA

View Full Version : [SOLVED] Why do create a temporary variable and then set the value to a function?



Benzadeus
08-15-2009, 09:10 AM
Why use



Function ExcelVersion() As String
Dim temp As String
On Error Resume Next
Select Case CLng(Application.Version)
Case 9: temp = "Excel 2000"
Case 10: temp = "Excel 2002"
Case 11: temp = "Excel 2003"
Case 12: temp = "Excel 2007"
Case 14: temp = "Excel 2010"
End Select
On Error GoTo 0
ExcelVersion = temp
End Function


instead of


Function ExcelVersion() As String
On Error Resume Next
Select Case CLng(Application.Version)
Case 9: ExcelVersion = "Excel 2000"
Case 10: ExcelVersion = "Excel 2002"
Case 11: ExcelVersion = "Excel 2003"
Case 12: ExcelVersion = "Excel 2007"
Case 14: ExcelVersion = "Excel 2010"
End Select
On Error GoTo 0
End Function

?

I've seen VBA programmers using the first topology more than the second... is there a reason?

Bob Phillips
08-15-2009, 09:19 AM
Probably just a style thing, some programmers don't like to use a function name as though it were a variable, they were probably raised on languages where you explicitly return a value, where you would have to use something like



Function ExcelVersion()
Dim temp As String
On Error Resume Next
Select Case CLng(Application.Version)
Case 9: temp = "Excel 2000"
Case 10: temp = "Excel 2002"
Case 11: temp = "Excel 2003"
Case 12: temp = "Excel 2007"
Case 14: temp = "Excel 2010"
End Select
On Error Goto 0
Return temp
End Function


Personally, I wouldn't do it the second way, although I will admit to using the function name where the value is only returned once.