tags:

views:

2115

answers:

5

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?

+1  A: 

There's no difference.

James Sun
+12  A: 

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
+5  A: 

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
+2  A: 

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
A: 

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 with out 'Call' keyword like

dim a as string

Test(a)

'a' will be empty string, after the call returns

if you call the function with 'Call' keyword like

dim a as string

Call Test(a)

'a' will contain the string 'Test'

The detailed explanation provided in the following link: http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx

Abraham
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
Indeed, and actually getting the result (for example, `MsgBox Test(a)`) restores the equivalence.
Ant