When do I use () after a function?

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!"

fn printHello = ( format "Hello World!\n")

RESULT:

printHello()

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 ()

printHello

RESULT:

printHello()

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:

FOR EXAMPLE:

Let's type the function name with ()

printHello()

RESULT:

Hello World!
OK

MAXScript executed the function, resulting in the string "Hello World!" being printed to the Listener, followed by the return value of the function which is OK.

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:

FOR EXAMPLE:

Let's define a custom function that prints "Hello World"

fn printAValue theValue = ( format "The Value Is %\n" theValue)

RESULT:

printAValue()

MAXScript is telling us that we have defined a new function and shows us how to call the function, but it does not tell us anything about the number of arguments yet. The return value only shows that a MAXScript function was defined.

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 ()

printAValue()

RESULT:

-- Argument count error: printAValue wanted 1, got 0

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:

printAValue(pi)

RESULT:

The Value Is 3.14159
OK

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:

printAValue pi

RESULT:

The Value Is 3.14159
OK

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.

FOR EXAMPLE:

Type in the constructor of a Box without ()

box

RESULT:

Box

MAXScript is telling us that 'box' contains the constructor function Box which, when called without parameters would make a new box object.

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 ()

box()

RESULT:

$Box:Box01 @ [0.000000,0.000000,0.000000]

MAXScript executes the Box constructor function without parameters, thus using defaults for all properties like position, size etc.

See Also