Creating Functions

Functions are defined in MAXScript using the function definition expression.

The syntax for <function_def> is:

[mapped] (function | fn) <name> { <parameter> } = <expr>

in which <name> is the name of the function.

The optional sequence of <parameter>s can be one of:


<name>: [<operand>] --keyword parameter with optional default value


and 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 will be set to the default value given, 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 will be 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.


function add a b = a + b

fn factorial n = if n <= 0 then 1 else n * factorial(n - 1)

mapped function rand_color x =

x.wireColor = random (color 0 0 0) (color 255 255 255)

fn starfield count extent:[200,200,200] pos:[0,0,0] =


local f = pos - extent / 2,

t = pos + extent / 2

for i = 1 to count do

sphere name:"star" \

radius:(random 0.1 2.0) \

position:(random f t) \

segs:4 smooth:false


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.

The form is:

showSource <fn>

This is useful for browsing definitions if you are working on many scripted functions, that were perhaps defined in several script files loaded with fileIn() or Run Script in the MAXScript utility.

See also

Function Variables

Function Parameters

The Return Expression

Special Notes about Function Calls