Functions are defined in MAXScript using the function definition expression.
The syntax for <function_def> is:
Here, <name> is the name of the function.
The optional sequence of <parameter> can be one of:
The <expr> after the '=' (equal sign) is the body of the function.
The function <name> is actually used to name a variable into which a value representing the function is placed.
When you call a function by using its name, you are accessing its definition in a variable of that name.
The scope of the function name variable is the current MAXScript scope.
The parameters for a function are either positional or keyword.
Positional parameters are defined with a simple <name> and must be placed before any keyword parameters.
Keyword parameters have a ':' (colon) after the name and an optional default value. The caller of the function can optionally supply keyword arguments in any order. Those not supplied is set to the given default value, or the special value unsupplied if a default value is not given.
Using the mapped prefix on a function definition marks this function to be automatically mapped over collections.
This means the function is automatically called repeatedly on the elements of a collection if the collection is given as the first argument to the function.
This allows you to define scripted functions that behave in a similar manner to the mapped built-in functions such as, copy, delete, move, and so on, which can be applied to an object set, path name pattern, or array.
See Collections for more information.
A scripted function returns as its value the value of the body <expr> . If the body is a block-expression, the function evaluates to the value of the last expression in the block.
A function can be called recursively (that is, the function can call itself), as is the factorial function in the previous examples.
If you need to locate the source of a scripted function, you can use the showSource() function. It displays a script Editor containing the source file in which the function was defined and positioned at the function's definition.
This is useful for browsing definitions if you are working on many scripted functions that were defined in several script files.