If you have worked on QTP with VB Script for sometime, I’m sure you have encountered this error Cannot use parentheses when calling a Sub. While at times you may have received this error because of a genuine mistake, most of the other times this maybe because of a quirk with VB Script.

Cannot use parentheses when calling a Sub

Let me show that with the help of an example.

I have created two functions

function oneargument(x) 
msgbox "In a function with a single argument " & x 
End function
function twoarguments(x,y) 
msgbox "In a function with two arguments " & x & " and " & y 
End function

What do you think will be the output of following code

function oneargument(x) 
msgbox "In a function with a single argument " & x 
End function

Call oneargument(10) 
oneargument(10)

Now guess the answer for this piece of code

function twoarguments(x,y) 
msgbox "In a function with two arguments " & x & " and " & y 
End function

Call twoarguments(10,20) 
twoarguments(10,20)

The two functions above are almost the same and here is what it gets interesting

The first piece of code above would run fine but the last call to twoarguments function will error out saying Cannot use parentheses when calling a Sub. While a call to a single argument function, with parenthesis works fine with or without using Call keyword, call to a function with more than one arguments using parenthesis will error out if you use parenthesis and omit Call keyword.

As a thumb rule, here are the ONLY 3 situations where you can use parenthesis safely with VBScript.

  1. Result = twoarguments(10,20) : When you retrieve the return value from a function with an assignment operator, you can have any number of arguments in parenthesis.
  2. Call twoarguments(10,20): When you use the Call keyword, you can have any number of arguments in parenthesis.
  3. oneargument(10): When you don’t use the Call keyword and you don’t have an assignment operator to retrieve return value, you can only have one argument in parenthesis.

Outside these 3, you should call functions/subroutines without using parenthesis.

Over to you, do  you know any other quirk with VB Script?