locked
When to use "call" when calling a function? RRS feed

  • Question

  • Is there a time when you must use "call" to invoke a function in vba?

    Like...

    call myfunction(x,6)

    thanks for any help.

    Friday, September 4, 2015 6:53 PM

Answers

  • In case you aren't aware, a sub routine does not return a value, a function does.  "Call" is more of a convenience for readability when calling a sub or function procedure.  However, its common usage is for calling sub routines.  When used and arguments are expected, the argument list must be enclosed in parentheses.  In short, it helps distinguish procedures that do not return a value from those that do, or functions that can return a value but the result isn't necessary at the point of usage.

    Alternatively, if you use a function that you don't expect a return value from, but prefer to enclose your argument list in parentheses, then you would use call instead of creating an assignment, such as in your example above.

    Call MyFunction(x, 6)

    Otherwise the following would be required if the argument list is enclosed in parentheses.

    variable=MyFunction(x, 6)

    Expecting that MyFunction is a function and not a sub routine, the function may not return a value, or the return value may not be needed where used.


    Friday, September 4, 2015 9:55 PM
  • Hi. One thing to remember is that using Call ignores the return value. So, I guess you would use Call if you don't want to worry about the return value of the function. Just my 2 cents...
    Friday, September 4, 2015 7:54 PM

All replies

  • Hi. One thing to remember is that using Call ignores the return value. So, I guess you would use Call if you don't want to worry about the return value of the function. Just my 2 cents...
    Friday, September 4, 2015 7:54 PM
  • Is there a time when you must use "call" to invoke a function in vba?

    Hi Fran,

    In addition to the answer of The DB Guy:

    I just did a little check, and in all my applications (70+), and I never use "Call", except in one test-application where I do some performance testing.

    So in my opinion it is not necessary to use "Call" in normal practice.

    Imb.

    Friday, September 4, 2015 9:23 PM
  • In case you aren't aware, a sub routine does not return a value, a function does.  "Call" is more of a convenience for readability when calling a sub or function procedure.  However, its common usage is for calling sub routines.  When used and arguments are expected, the argument list must be enclosed in parentheses.  In short, it helps distinguish procedures that do not return a value from those that do, or functions that can return a value but the result isn't necessary at the point of usage.

    Alternatively, if you use a function that you don't expect a return value from, but prefer to enclose your argument list in parentheses, then you would use call instead of creating an assignment, such as in your example above.

    Call MyFunction(x, 6)

    Otherwise the following would be required if the argument list is enclosed in parentheses.

    variable=MyFunction(x, 6)

    Expecting that MyFunction is a function and not a sub routine, the function may not return a value, or the return value may not be needed where used.


    Friday, September 4, 2015 9:55 PM
  • To amplify what RunningManHD has said, I tend to use Call as a stylistic preference. The "naked" subroutine calling syntax in Basic can feel anachronistic if you are used to programming in C-like languages (where there is no formal distinction between a subroutine and a function). I find my code to be more readable and to feel more "correct" when I always pass parameters within parentheses. But it's strictly an optional choice to do so.
    Sunday, September 6, 2015 4:53 PM