When to use parentheses to enclose subroutine and function arguments

Article contributed by Jonathan West

The rules are confusing concerning the use of parentheses to enclose argument lists. I have even seen MS Knowledgebase articles that have got it wrong. The rules are as follows.

1.

For the initial line of a Sub or Function, you use parentheses to enclose the arguments (if any), e.g.

Sub MySubroutine(a As Long, b As String)

or

Function MyFunction(a As Long, B As String) As Long

2.

When calling a function, you use parentheses to enclose the arguments, like this.

x = MyFunction(a:=1, b:="abc")

or

x = MyFunction(1, "abc")

3.

When calling a sub directly, you don't use parentheses, like this.

MySub a:=1, b:="abc"

or

MySub 1, "abc"

 

4.

When calling a sub using the Call keyword, you do use parentheses (this made sense to somebody!)

Call MySub(a:=1, b:="abc")

or

Call MySub(1, "abc")

Note that it doesn't matter whether or not you use the Call keyword to call a subroutine. The effect on the program is identical whether or not you use Call (assuming you have the parentheses right). Most of the code on this site doesn't use Call, simply because it is fewer words to type.

5.

When dealing with methods of an object, use the same rules, depending on whether you are obtaining a return value from the method (like a function), or just applying the method to the object without a return value (like a subroutine). For example, on the one hand:

Selection.GoTo What:=wdGoToPage, Name:="5"

But on the other hand (because you're now using a function which returns a value):

Dim MyRange As Range
Set MyRange = ActiveDocument.Range
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:="5")


Click to view Terms of Use page

Click to view Disclaimer page