Knowledge Base Articles » KB100222: When to Use Parentheses with a Subroutine or Function in VB/VBA/VBScript.
When to use, or not to use, parentheses
( ) with a VB, VBA, or VBScript subroutine or function can be a bit
confusing. We will first examine how to set up the code structure for a
subroutine and function, and then we will look at how to call that subroutine
or function.
Subroutine Code Structure
If a subroutine does not receive any arguments, then you do not use any
parentheses when declaring the subroutine. Note the required use of the
Sub and End Sub statements which define the start and finish of the subroutine
code.
Sub MySubroutine
'place your code here
End Sub
However, if a subroutine does receive arguments, then you must use parentheses
in its declaration. If there is more than one argument, you must separate
the arguments with commas.
Sub MySubroutine(intUsageFee, intTimeInHours, strCompanyName)
'place your code here
End Sub
Function Code Structure
As with subroutines, if a function does not receive any arguments, then
you do not use any parentheses in the function declaration. Note the required
use of the
Function and
End Function statements which define
the start and finish of the function code.
Function MyFunction
'place your code here
End Function
However, if a function does receive arguments, then you must enclose
its argument list in parentheses when declaring the function. If there
is more than one argument, you must separate the arguments with commas.
Function MyFunction(intUsageFee, intTimeInHours,
strCompanyName)
'place your code here
End Function
Calling the Subroutine
There are two possible ways to call a subroutine. You may either call
the subroutine directly by name only, or you may call it by using the
VBScript
Call statement.
Calling a Subroutine by Name
If a subroutine does not pass any arguments, then you do not use any parentheses.
MySubroutine
Even if a subroutine has arguments, you still do not use parentheses.
If there is more than one argument, you must separate the arguments with
commas. If you enclose the argument list in parentheses when attempting
to call a subroutine that takes more than one argument, you will receive
the runtime error "Cannot use parentheses when calling a Sub".
MySubroutine intUsageFee, intTimeInHours, "DevGuru"
Using the VBScript Call Statement to Call a Subroutine
The use of
Call statement is optional when you wish to call a subroutine.
The purpose of the Call statement when used with a Sub is to allow you
to enclose the argument list in parentheses. However, if a subroutine
does not pass any arguments, then you still should not use parentheses
when calling a Sub using the Call statement.
Call MySubroutine
If a subroutine has arguments, you must use parentheses when using the
Call statement. If there is more than one argument, you must separate
the arguments with commas.
Call MySubroutine(intUsageFee, intTimeInHours, "DevGuru")
Calling the Function
There are two possible ways to call a function. You may either call the
function directly, by name only, or you may call it by using the VBScript
Call statement.
Calling a Function by Name
When calling a function directly by name and when there is no assignment to a returned value,
all of the following are legal syntax:
MyFunction
MyFunction()
MyFunction intUsageFee, intTimeInHours, "DevGuru"
If you want a returned value, you can assign the function to a variable.
Note that if there is one or more arguments, you must use the parentheses.
returnval = MyFunction
returnval = MyFunction()
returnval = MyFunction(intUsageFee, intTimeInHours, "DevGuru")
Using the Call Statement
If a function has arguments, you must use parentheses when using the
Call statement.
If there is more than one argument, you must separate the arguments with commas.
There is no returned value when using
Call.
Call MyFunction(intUsageFee, intTimeInHours, "DevGuru")