MAXScript FAQ > When do I use () after a function? |
A user-defined MAXScript function is stored in a variable which points at a location in memory where the function definition is stored.
Some functions take no arguments (parameters, values passes to the function to operate on), others can expect one or more arguments.
FOR EXAMPLE: |
Let's define a custom function that prints "Hello World!" |
RESULT: |
MAXScript is telling us that we have defined a new function and shows us how to call the function. |
If we type in only the name of the function, we are not actually calling the function but asking MAXScript to print the function value itself.
FOR EXAMPLE: |
Let's type in only the name of the function without () |
RESULT: |
MAXScript is showing us that the variable printHello points at a definition of a function that can be called as shown. |
Since our function does not take any arguments to operate on, we can just do as instructed and call the function:
If we would define a function that expects one argument, we could still use the parentheses around the argument. In some other languages, the parentheses around the arguments are mandatory. In such languages, the () form is a special case of the argument passing, just without any arguments.
In the case of MAXScript though, the parentheses are optional except when no arguments are passed to the function:
Now if we would try to call the function like before with () but no argument, the function will tell us what it expects in an error message:
FOR EXAMPLE: |
Let's type the function name with () |
RESULT: |
MAXScript tries to execute the function, but finds that the arguments are not enough and complains. |
We can put the argument value inside the parentheses to make the function work as desired:
FOR EXAMPLE: |
Let's provide the argument inside the parentheses: |
RESULT: |
MAXScript executes the function and finds that the arguments are enough, so the function produces the correct result. |
But as mentioned, MAXScript does not mandate the parentheses around an argument, so we can omit them.
FOR EXAMPLE: |
Let's provide the argument without the parentheses: |
RESULT: |
As we can see, the result is the same. |
The same applies to functions implemented by plugins and by MAXScript itself, like for example the constructor functions of geometry primitives.
In order to call the function without any arguments, we have to tell the function that no parameters are going to be passed to it.
To do this, we add the pair of parentheses to the end of the function name like we did with the user-defined function above.
FOR EXAMPLE: |
Type in the constructor of a Box with () |
RESULT: |
MAXScript executes the Box constructor function without parameters, thus using defaults for all properties like position, size etc. |