Difference between revisions of "Parentheses"
m (added example of omitting parentheses on a function call statement) |
(added new example) |
||
Line 19: | Line 19: | ||
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>. | 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>. | ||
− | == | + | == 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]] 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. | ||
Line 28: | Line 28: | ||
end for | end for | ||
print "Boom!"</source> | print "Boom!"</source> | ||
+ | |||
+ | The next example shows that [[Insert|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. | ||
+ | |||
+ | <source lang="miniscript>a = ["heidy", "neighbor"] | ||
+ | a.insert 1, "ho"</source> | ||
[[Category:Language]] | [[Category:Language]] | ||
[[Category:Conventions]] | [[Category:Conventions]] |
Revision as of 20:07, 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
.
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.
for i in range(10, 1)
print i
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"