+ Reply to Thread
Results 1 to 9 of 9

Best way to return a value from a function?

  1. #1
    Registered User
    Join Date
    08-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    76

    Best way to return a value from a function?

    I am very new to VBA, and I wanted to perform the painfully simple task of returning a value from a function. After a lot of searching, the only way I've found to do it is like this:

    Please Login or Register  to view this content.
    I've been programming in a number of different languages for years, and never in my born days have I seen anything like that!!

    And this gives "syntax error":

    Please Login or Register  to view this content.
    Please can someone explain why VBA works like that? Although it seems crazy, I am sure there's a logical reason for it. IME, there are usually logical explanations for things which, at first sight, seem nuts!

  2. #2
    Registered User
    Join Date
    02-18-2014
    Location
    France
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Best way to return a value from a function?

    I think the second method (C like) would have worked in previous excel's versions. No longer.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Best way to return a value from a function?

    Why does it seem nuts?

    I've seen both methods used in different languages.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    08-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: Best way to return a value from a function?

    Quote Originally Posted by Norie View Post
    Why does it seem nuts?

    I've seen both methods used in different languages.
    I'd be interested to know which other languages the VBA method is used it. I've coded PHP, C, C++, Java, Javascript, Python and F77 and I've never seen it.

    The main problem that I see with it is that if you want to change the function name, you have to change it in two places. If you use the Return syntax, then the name need only be changed in one place.

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Best way to return a value from a function?

    Return is used to branch back to procedure from GoSub statement.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Best way to return a value from a function?

    Admittedly there aren't many that use the exact same syntax, off the top of my head I can think of Pascal and VBS.

    There are languages that don't require the use of return, for example Ruby where the value from the last evaluated expression is returned if return is omitted.

    Perhaps the reason VBA doesn't use it is because it already uses Return with GoSub?

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Best way to return a value from a function?

    VB 3/4/5/6 also.

  8. #8
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: Best way to return a value from a function?

    Quote Originally Posted by XmisterIS View Post
    I'd be interested to know which other languages the VBA method is used it. I've coded PHP, C, C++, Java, Javascript, Python and F77 and I've never seen it.

    The main problem that I see with it is that if you want to change the function name, you have to change it in two places. If you use the Return syntax, then the name need only be changed in one place.
    Every time i need to major an important change to a keyword, i use replace in the text editor of the programming tool.

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Best way to return a value from a function?

    I probably shouldn't even venture into this one. Being more mathematician/scientist, I am not the most experienced programmer.
    I'd be interested to know which other languages the VBA method is used it. I've coded PHP, C, C++, Java, Javascript, Python and F77 and I've never seen it.
    I find it interesting that you have not seen this in Fortran. While I almost certainly do not have the experience you have, my experience is that Fortran, like VBA, uses this same strategy for returning values from Functions. A sample Fortran function from Fortran 77 with Numerical Methods for Engineers and Scientists by D. M. Etter:
    Please Login or Register  to view this content.
    I cannot speak to the philosophy behind this strategy for returning values, but it seems to be the common strategy in the few languages I'm familiar with. I tend to just chalk it up to the nuances of going from one language to another -- each language has its language specific syntax that just has to be lived with.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 03-13-2013, 12:38 PM
  2. Return zero if UDF-function return error
    By mkvassh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-14-2010, 08:56 AM
  3. IF function does not return blank in function cell
    By superdow in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-02-2009, 05:11 PM
  4. Function to return value
    By Eirik Sævareid in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-25-2006, 04:20 PM
  5. INTRATE function-what interest rate does this function return?
    By gfoster07k in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-31-2005, 03:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1