Function - MsgBox - Vs MsgBox in VBScript - Stack Overflow
Function - MsgBox - Vs MsgBox in VBScript - Stack Overflow
I'm trying to write a VBScript and I'm using functions such as Randomize, and MsgBox. I'm
curious as to what is the difference of using () and not using them. For example:
29
Randomize - This line works.
5
MsgBox "Hello world!" - This works.
The script will be running on multiple machines with different versions of Windows (at least
Windows XP). I'm wondering if I would be getting any compatibility/syntax issues in using
these functions.
function vbscript
Share Follow edited Nov 10 '15 at 3:28 asked Nov 29 '12 at 7:23
Pang user1862387
8,748 144 80 114 291 1 3 3
A callable piece of code (routine) can be a Sub (called for a side effect/what it
does) or a
Function (called for its return value) or a mixture of both. As the docs
for MsgBox
27
Displays a message in a dialog box, waits for the user to click a
button, and returns a
value indicating which button the user clicked.
If you want to display a message to the user and need to know the user's
reponse:
Dim MyVar
If you want to display a message to the user and are not interested
in the response:
The design flaw of using () for parameter lists and to force call-by-value
semantics
>> n = 1
>> S n
>> WScript.Echo n
>> S (n)
>> WScript.Echo n
>>
S (n) does not mean "call S with n", but "call S with a copy of n's value".
Programmers seeing
that
>> s = "value"
>> MsgBox(s)
>>
The compiler's leniency with regard to empty () in a Sub call. The 'pure'
Sub Randomize (called
for the side effect of setting the random seed) can
be called by
Randomize()
although the () can neither mean "give me your return value) nor "pass
something by value". A
bit more strictness here would force prgrammers to
be aware of the difference in
Randomize n
and
Randomize (n)
2 I would recommend using vbOkCancel instead of hard coding 65. It's more readable.
– Matthieu Cormier
Jan 16 '14 at 14:58
I understand how to use this, but I'd like to get more clarity. Is MsgBox a function or a Sub or a Hybrid
of some kind?
– Ejaz Ahmed
Jan 21 '16 at 4:34
To my knowledge these are the rules for calling subroutines and functions in VBScript:
10 When calling a subroutine or a function where you discard the return value don't use
parenthesis
When calling a function where you assign or use the return value enclose the arguments
in parenthesis
When calling a subroutine using the Call keyword enclose the arguments in parenthesis
Since you probably wont be using the Call keyword you only need to learn the rule that if
you call a function and want to assign or use the return value you need to enclose the
arguments in parenthesis. Otherwise, don't use parenthesis.
I'm quite sure that these syntactic rules applies across different versions of operating systems.
Share Follow edited Oct 17 '15 at 9:27 answered Nov 29 '12 at 8:51
Martin Liversage
97.9k 20 196 239
You are just using a single parameter inside the function hence it is working fine in both the
cases like follows:
3
MsgBox "Hello world!"
But when you'll use more than one parameter, In VBScript method will parenthesis will throw
an error and without parenthesis will work fine like:
Share Follow edited Nov 29 '12 at 17:49 answered Nov 29 '12 at 17:22
Shivam Gupta
419 3 15
Thanks @Ekkehard.Horner!! I had described it in a simple manner( or you may say in a lay man's
language). What you want me to modify in it. Please elaborate. Thanks in advance!!
– Shivam Gupta
Nov 29 '12 at 17:40
your last example is just a copy of the working one, so it won't throw an error.
– Ekkehard.Horner
Nov
29 '12 at 17:44
Hey Thanks @Ekkehard.Horner for warning me. I got your point and rectified my answer. I forgot to put
parenthesis in my last example. Thank you so much!!
– Shivam Gupta
Nov 29 '12 at 17:50
just FYI, MsgBox with parentheses and multiple variables will work, i.e. if you are assigning it to a value
– freginold
Dec 13 '16 at 14:05
You have to distinct sub routines and functions in vba... Generally (as far as I know), sub
routines do not return anything and the surrounding parantheses are optional. For functions,
1 you need to write the parantheses.
As for your example, MsgBox is not a function but a sub routine and therefore the parantheses
are optional in that case. One exception with functions is, when you do not assign the
returned value, or when the function does not consume a parameter, you can leave away the
parantheses too.
This answer goes into a bit more detail, but basically you should be on the save side, when
you provide parantheses for functions and leave them away for sub routines.
Share Follow edited May 23 '17 at 12:10 answered Nov 29 '12 at 8:12
Community ♦ Tom
1 1 116 2
MsgBox is just one example for a routine that can be used as a Sub or a Function; parameter list () are
forbidden in Sub calls; 'consume' is a misleading term when applied to parameter passing semantics.
– Ekkehard.Horner
Nov 29 '12 at 9:23
Sub = MsgBox("Msg",vbYesNo,vbCritical,"Title")
Case = vbYes
Case = vbNo
End Select
vs Normal:
MsgBox"This is normal"
Share Follow edited Jan 9 '20 at 18:05 answered Jan 9 '20 at 18:02
LarsTech Gladius125
77.7k 14 136 206 21 3