Results 1 to 8 of 8

Thread: [RESOLVED] Using function name as return value

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Resolved [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:
    1. 'Example #1
    2. Private Function GenerateString(ByVal Length As Long) As String
    3.    
    4.     Dim l As Long
    5.    
    6.     For l = 1 To Length
    7.         GenerateString = GenerateString & Chr$(Int(Rnd * 255) + 1)
    8.     Next l
    9.    
    10. End Function
    11.  
    12. 'Example #2
    13. Private Function GenerateString(ByVal Length As Long) As String
    14.    
    15.     Dim l As Long
    16.    
    17.     GenerateString = Space$(Length)
    18.    
    19.     For l = 1 To Length
    20.         Mid$(GenerateString, l, 1) = Chr$(Int(Rnd * 255) + 1)
    21.     Next l
    22.    
    23. End Function

    vs.

    vb Code:
    1. Private Function GenerateString(ByVal Length As Long) As String
    2.    
    3.     Dim l As Long, strReturn As String
    4.    
    5.     For l = 1 To Length
    6.         strReturn = strReturn & Chr$(Int(Rnd * 255) + 1)
    7.     Next l
    8.    
    9.     GenerateString = strReturn
    10.    
    11. End Function
    Last edited by DigiRev; Sep 22nd, 2009 at 07:40 AM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

  4. #4

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Using function name as return value

    Quote Originally Posted by MartinLiss View Post
    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.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Last edited by LaVolpe; Sep 23rd, 2009 at 08:35 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: [RESOLVED] Using function name as return value

    Quote Originally Posted by LaVolpe View Post
    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 View Post
    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?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width