Parenthesis - to use or not to use?

J

John Coleman

Greetings all

Quick question - In John Green et al's Excel 2000 VBA I find the
following assertion: "The general rule is that if you want to capture
the return value of a function, you need to put any argument in
parenthesis. If you don't want to use the return value, you should not
use parenthesis."

The first sentence is of course a nonnegotiable point of syntax - but
what about the second sentence? I was under the impression that it is
100% a matter of taste if I write msgbox "Hello world" or
msgbox("Hello world"), but the word "should" above suggests that there
is a principled reason to drop parenthesis if not needed. Is there? In
particular, does the computer in some sense process a non-returned
value if you include parenthesis but not otherwise, so that a small
performance hit attaches itself to parenthesis? Also, what about sub
calls when there is no return value to worry about?
If it is just a matter of taste I'll stick to parenthesis since it
seems somehow more logical to me.

Thank you for your time.

-John Coleman
 
L

Libby

I would say that if your subsequent code depends on a
response from the user, eg:
if msgbox ("Hello World?", vbquestion+vbyesno) = vbyes then
then you need to use parenthesis or you get a syntax
error, but if you are displaying the msgbox for
information purposes then they are not necessary.
eg
msgbox "Hello World", vbinformation
 
B

Bernie Deitrick

John,

You, of course, can do what you want, but you should consider the future
readers of your code. If I am looking at code and see

MsgBox(.....

then I start looking for the return value and its use. Without the ( )s I
know that it is only being used for display purposes.

HTH,
Bernie
MS Excel MVP
 
T

Toby Erkson

I believe MSGBOX was an example of using parenthesis and not the focus. In
the second paragraph read the last three sentences and you'll actually
understand the question at hand.
 
T

Tom Ogilvy

It isn't a matter of taste. It is part of the syntax of the language.

In your example, there is no problem, but by using parentheses in a
situation where a value is not being returned, you implicitly pass the
argument byVal rather than the default byRef. If you are passing an object
or any argument you want to be byRef this could have deleterious effects.
The bottom line is that arguments encased in parentheses are evaluated
before they are passed. Now you may want that to happen, but if so, it
would be clearer to declare the argument as byVal in the function
declaration.
 
H

Harald Staff

Hi John

Adding to Tom's explanation. It's a question of language syntax, and you
have limited choices of personal preferences. This is valid:

MsgBox "Hello world", vbYesNo

This is valid:

Dim M
M = MsgBox("Hello world", vbYesNo)

This will err:

Dim M
M = MsgBox "Hello world", vbYesNo

and this will err:

MsgBox ("Hello world", vbYesNo)

Difference of use is like this:

Sub tester()
Dim M
'display only :
MsgBox "Yo da man", vbYesNo
MsgBox "I have no idea what you replied"
'capture and evaluate :
M = MsgBox("Yo da man", vbYesNo)
If M = vbYes Then
MsgBox "you said yes"
Else
MsgBox "you said no"
End If
End Sub

HTH. Best wishes Harald
 
J

John Coleman

Thanks to everyone for their replies. I figured out on the nature of
my error. For my own subroutines I always call them using the call
keyword, in which parenthesis are required. I pretty much use message
boxes only for debugging purposes, and with 1 parameter () are
ignored. A little experimentation shows that 2 or more parameters with
() as I have been doing leads to problems.

My way to think about it now - msgbox( , , ..) is a noun that must be
embedded in a bigger context to make sense, but in msgbox "hello
world" msgbox is now functioning as a verb.

In my defense - when I type msgbox in the VBA editor the IntelliSense
which pops up suggests (), and sinse my code was compiling no-problem
I didn't realize I was being ungrammatical. It is strange how you can
misunderstand a basic feature of a language but nevertheless get by
with your own idiosyncratic work-arounds.

sub thanks()
MsgBox Prompt := "Now I understand", Title:= "Thanks again"
end sub

-John Coleman
 
Top