Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2005

Tip: Looking for answers? Try searching our database.

Function return value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jay - 16 Sep 2005 08:54 GMT
In a VBA function, a value is returned by assigning the function name
to the value. Can this be done in several places (CODE EXAMPLE 1), or
should it only be done once at the end of the function, with a
variable being used to temporarily hold the return value (CODE EXAMPLE
2)?

CODE EXAMPLE 1
Function Test() as integer
 Test = 0
 If ??? then
   Test = 1
   Exit Function
 End if
 ...
End Function

CODE EXAMPLE 2
Function Test() as integer
 Dim ReturnVal as integer
 ReturnVal = 0
 If ??? then
   ReturnVal = 1
   Goto EndFunc
 End if
 ...
EndFunc:
 Test = ReturnVal
End Function
Jay Freedman - 16 Sep 2005 16:01 GMT
The technical answer is, it *can* be done in several places; that is,
example 1 will work.

The answer from experience is, it *should* be done only in one place, for a
couple of reasons. The main reason is to help prevent bugs, or to ease
debugging when the inevitable happens. This goes together with the general
rule that, except for error-handling, there should be only one exit point
for each sub/function.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> In a VBA function, a value is returned by assigning the function name
> to the value. Can this be done in several places (CODE EXAMPLE 1), or
[quoted text clipped - 24 lines]
>   Test = ReturnVal
> End Function
Jonathan West - 18 Sep 2005 12:17 GMT
> The technical answer is, it *can* be done in several places; that is,
> example 1 will work.
[quoted text clipped - 5 lines]
> rule that, except for error-handling, there should be only one exit point
> for each sub/function.

Personally, I take a more relaxed view of this. I think that Example 1 is
perfectly good code, and reduces the amount of jumping about within a
routine and reduces the total number of lines of code.

There are cases where a single exit point would be advisable, such as where
there is a need to restore global parameters (such as the position of the
Selection) prior to exit. These issues need to be addressed on a
case-by-case basis.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org 

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.