33

There's some code in our project that looks a bit like this:

Private Sub Method1()
    Call InnerMethod
End Sub

Private Sub Method2()
    InnerMethod
End Sub

Private Sub InnerMethod()
    '' stuff
End Sub

What's the advantage of doing Method1 over Method2?

dance2die
  • 35,807
  • 39
  • 131
  • 194
Ant
  • 5,150
  • 2
  • 33
  • 41
  • Does this answer your question? [Should I use Call keyword in VB/VBA?](https://stackoverflow.com/questions/2573597/should-i-use-call-keyword-in-vb-vba) – StayOnTarget Apr 15 '23 at 15:02
  • No more than the answers to this question, to be honest! Fortunately for me all of that VB code is *long* dead. Even the other question you refer to is from 13 years ago - more than a year after this one was asked! Thanks for the suggestion though. – Ant Apr 16 '23 at 22:41

5 Answers5

37

From the MSDN:

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

For example:

Sub Proc1()
    Debug.Print "Hello World"
End Sub

Sub Proc2(text As String)
    Debug.Print "Hello " & text
End Sub

In the immediate window, if you enter

Proc1

then "Hello World" prints. If you enter

Call Proc1

then "Hello World" prints. If you enter

Proc2 "World"

then "Hello World" prints. If you enter

Call Proc2 "World" 

you get a compile error. You would have to enter

Call Proc2("World")
Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
11

Call does nothing special other than call the method. It is a hang over from the old days of Basic when all lines had to start with a keyword. "Let" is another of these keywords, which was always put before an assignment, but is no longer required.

Method1 and Method2 do the exact same thing.

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
4

I have found a major difference about 'call' keyword with functions that having, ByRef Arguments (I have found this in MS-Access VBA editor). If you are calling the function without 'Call' keyword, ByRef aruments will not set for the calle. For Ex:

Private Function Test(Optional ByRef refArg As String) As Boolean    
    refArg = "Test"
    Test = True
End Function

If you call the function without the Call keyword like

Dim a As String
Test(a)

a will be an empty string, after the call returns

If you call the function with the Call keyword like

Dim a As String
Call Test(a)

a will contain the string Test

The detailed explanation provided in the following link: Cannot use parentheses when calling a Sub

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Abraham
  • 49
  • 1
  • 2
  • 5
    The two are not equivalent. In the first case, enclosing the argument in parens causes it to be evaluated, and the result of the evaluation passed: in essence, you're passing a copy of "a", not "a" itself. This is made obvious by the parser. When you enter Test(a) and leave the line, the parser will add a space, making it Test (a) to indicate that it's an expression; whereas with Call Test(a) no space will be added -- you're passing "a" directly. See the answer by Patrick Cuff above for the doc reference. – Jim Mack Jul 01 '10 at 20:21
  • Indeed, and actually getting the result (for example, `MsgBox Test(a)`) restores the equivalence. – Ant Jul 05 '10 at 10:21
  • You should delete this answer. It is extremely misleading... and wrong. Down-voted. Never call a procedure with parentheses around the argument list... with two exceptions: using the Call keyword or doing an assignment from the result of a function. You are not doing either and so the variable surrounded by parentheses gets evaluated and sent to the procedure as a literal value (not the variable) and since it is no longer a variable, there is no way to get the updated value back. You don't need Call in your example... you just need to remove the parentheses from around the argument: `Test a` – Excel Hero Dec 31 '20 at 23:47
2

There's no difference.

James Sun
  • 1,461
  • 2
  • 19
  • 26
2

Here's a post which describes when you need to use call vs not using it and when to parentheses around your parameters.

You can also read more about call from MSDN. Essentially the main difference is that when you use call to call a function you can't access the return value.

JoshBerke
  • 66,142
  • 25
  • 126
  • 164