Difference between revisions of "Parentheses"
m (added example of omitting parentheses on a function call statement) |
|||
Line 17: | Line 17: | ||
<source lang="miniscript">x = time</source> | <source lang="miniscript">x = time</source> | ||
− | Finally, as mentioned above, you don't need parentheses around the arguments of a function that is the very first thing on a statement. | + | 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 <c>print(42)</c>, simply write <c>print 42</c>. |
== Example == | == Example == |
Revision as of 13:32, 15 December 2020
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.
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
.
Example
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.
for i in range(10, 1)
print i
wait
end for
print "Boom!"