Difference between revisions of "Parentheses"

From MiniScript Wiki
Jump to navigation Jump to search
(added new example)
(Noted that parens are only needed in a function call if it takes parameters)
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
 
<source lang="miniscript">print cos(0)  // parens needed; cannot just say: print cos 0</source>
 
<source lang="miniscript">print cos(0)  // parens needed; cannot just say: print cos 0</source>
  
3. Use them when declaring a [[function]].
+
3. Use them when declaring a [[function]]. (As of MiniScript 1.5.1, these are needed only if the function takes parameters.)
  
 
And that's it.
 
And that's it.
Line 21: Line 21:
 
== Examples ==
 
== Examples ==
  
The following example prints ten numbers, waiting one second each, and then prints a message.  Notice how [[print]] and [[wait]] are called without any parentheses.  But on the [[range]] call, because it has arguments and is used as part of a larger statement, parentheses are needed.
+
The following example prints ten numbers, waiting one second each, and then prints a message.  Notice how [[print]], [[rnd]], and [[wait]] are called without any parentheses.  But on the [[range]] call, because it has arguments and is used as part of a larger statement, parentheses are needed.
  
 
<source lang="miniscript">for i in range(10, 1)
 
<source lang="miniscript">for i in range(10, 1)
   print i
+
   print i * rnd
 
   wait
 
   wait
 
end for
 
end for

Latest revision as of 13:27, 1 January 2022

Parentheses in MiniScript have only three uses:

1. Use them to group math operations in the order you want them, just as in algebra.

x = (2+4)*7   // this is different from 2+4*7

2. Use them around the arguments in a function call, except when the function call is the entire statement.

print cos(0)   // parens needed; cannot just say: print cos 0

3. Use them when declaring a function. (As of MiniScript 1.5.1, these are needed only if the function takes parameters.)

And that's it.

Since other languages often require parentheses in many other places, it’s worth pointing out where parentheses are not used in MiniScript. First, don’t put parentheses around the condition of an if or while statement. Second, parentheses are not needed (and should be omitted) when calling a function without any arguments. For example, there is a time function that gets the number of seconds since the program began. It doesn’t need any arguments, so you can invoke it without parentheses.

x = time

Finally, as mentioned above, you don't need parentheses around the arguments of a function that is the very first thing on a statement. A common example is the print function; instead of print(42), simply write print 42.

Examples

The following example prints ten numbers, waiting one second each, and then prints a message. Notice how print, rnd, and wait are called without any parentheses. But on the range call, because it has arguments and is used as part of a larger statement, parentheses are needed.

for i in range(10, 1)
  print i * rnd
  wait
end for
print "Boom!"

The next example shows that list.insert doesn't need parentheses either, if you're calling it as a statement by itself. When there are multiple arguments, they are separated by commas as usual.

a = ["heidy", "neighbor"]
a.insert 1, "ho"