Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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</p> <blockquote> <p>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.</p> <p>MsgBox(prompt[, buttons][, title][, helpfile, context])</p> </blockquote> <p>indicate, this routine is of the third kind.</p> <p>The syntactical rules of VBScript are simple:</p> <p><strong>Use parameter list () when calling a (routine as a) Function</strong></p> <p>If you want to display a message to the user and need to know the user's reponse:</p> <pre><code>Dim MyVar MyVar = MsgBox ("Hello World!", 65, "MsgBox Example") ' MyVar contains either 1 or 2, depending on which button is clicked. </code></pre> <p><strong>Don't use parameter list () when calling a (routine as a) Sub</strong></p> <p>If you want to display a message to the user and are not interested in the response:</p> <pre><code>MsgBox "Hello World!", 65, "MsgBox Example" </code></pre> <p>This beautiful simplicity is messed up by:</p> <p>The design flaw of using () for parameter lists and to force call-by-value semantics</p> <pre><code>&gt;&gt; Sub S(n) : n = n + 1 : End Sub &gt;&gt; n = 1 &gt;&gt; S n &gt;&gt; WScript.Echo n &gt;&gt; S (n) &gt;&gt; WScript.Echo n &gt;&gt; 2 2 </code></pre> <p>S (n) does not mean "call S with n", but "call S with a copy of n's value". Programmers seeing that</p> <pre><code>&gt;&gt; s = "value" &gt;&gt; MsgBox(s) </code></pre> <p>'works' are in for a suprise when they try:</p> <pre><code>&gt;&gt; MsgBox(s, 65, "MsgBox Example") &gt;&gt; Error Number: 1044 Error Description: Cannot use parentheses when calling a Sub </code></pre> <p>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</p> <pre><code>Randomize() </code></pre> <p>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</p> <pre><code>Randomize n </code></pre> <p>and</p> <pre><code>Randomize (n) </code></pre> <p>The Call statement that allows parameter list () in Sub calls:</p> <blockquote> <blockquote> <p>s = "value" Call MsgBox(s, 65, "MsgBox Example")</p> </blockquote> </blockquote> <p>which further encourage programmers to use () without thinking.</p> <p>(Based on <a href="http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx">What do you mean "cannot use parentheses?"</a>)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload