OpenSCAD User Manual/User-Defined Functions and Modules

Users can extend the language by defining their own functions and modules. This allows grouping portions of script for easy reuse with different values. Well chosen names also help document your script.

Functions return values.

Modules perform actions, but do not return values.

OpenSCAD calculates the value of variables at compile-time, not run-time. The last variable assignment within a scope applies everywhere in that scope. It also applies to any inner scopes, or children, thereof. See Scope of variables for more details. It may be helpful to think of them as override-able constants rather than as variables.

For functions and modules OpenSCAD makes copies of pertinent portions of the script for each use. Each copy has its own scope, which contains fixed values for variables and expressions unique to that instance.

The name of functions and modules is case sensitive, therefore test and TEST refer to different functions/modules.

Scope
Modules and functions can be defined within a module definition, where they are visible only in the scope of that module.

For example

The function y and module plot cannot be called in the global scope.

Functions
Functions operate on values to calculate and return new values. function name ( parameters ) = value ;
 * function definition
 * name
 * Your name for this function. A meaningful name is helpful later.  Currently valid names can only be composed of simple characters and underscores [a-zA-Z0-9_] and do not allow high-ascii or unicode characters.


 * parameters
 * Zero or more arguments. Parameters can be assigned default values, to use in case they are omitted in the call. Parameter names are local and do not conflict with external variables of the same name.


 * value
 * an expression that calculates a value. This value can be a vector.

Function use
When used, functions are treated as values, and do not themselves end with a semicolon.

 You can also use the let statement to create variables in a function: It can be used to store values in recursive functions. See the wikipedia page for more information on the general concept.

Recursive functions
Recursive function calls are supported. Using the Conditional Operator "... ? ... : ... ", it is possible to ensure the recursion is terminated. There is a built-in recursion limit to prevent an application crash (a few thousands). If the limit is hit, you get an error like: ERROR: Recursion detected calling function ... .

For all tail-recursive functions that calls itself, OpenSCAD is able to eliminate internally the recursion transforming it in an iterative loop. The previous example code is not a tail call as an "add" operation need to be calculated after calling. But the following is entitled to tail-recursion elimination: Tail-recursion elimination allows much higher recursion limits (up to 1000000).

Function literals
Function literals are expressions that define functions, other names for this are lambdas or closures.
 * function literal

function (x) x + x

Function literals can be assigned to variables and passed around like any value. Calling the function uses the normal function call syntax with parenthesis. func = function (x) x * x; echo(func(5)); // ECHO: 25

It's possible to define functions that return functions. Unbound variables are captured by lexical scope. a = 1; selector = function (which) which == "add" ? function (x) x + x + a             : function (x) x * x + a; echo(selector("add"));     // ECHO: function(x) ((x + x) + a) echo(selector("add")(5));  // ECHO: 11 echo(selector("mul"));    // ECHO: function(x) ((x * x) + a) echo(selector("mul")(5));  // ECHO: 26

Overwriting built-in functions
It is possible to overwrite the built-in functions. Note that definitions are handled first, so the evaluation does indeed return  for both   calls as those are evaluated in a later processing step.

Modules
Modules can be used to define objects or, using, define operators. Once defined, modules are temporarily added to the language.

module name ( parameters ) { actions }
 * module definition
 * name
 * Your name for this module. Try to pick something meaningful. Currently valid names can only be composed of simple characters and underscores [a-zA-Z0-9_] and do not allow high-ascii or unicode characters.


 * parameters
 * Zero or more arguments. Parameters may be assigned default values, to use in case they are omitted in the call. Parameter names are local and do not conflict with external variables of the same name.


 * actions
 * Nearly any statement valid outside a module can be included within a module. This includes the definition of functions and other modules. Such functions and modules can be called only from within the enclosing module.

Variables can be assigned, but their scope is limited to within each individual use of the module. There is no mechanism in OpenSCAD for modules to return values to the outside. See Scope of variables for more details.

Object modules
Object modules use one or more primitives, with associated operators, to define new objects.

In use, object modules are actions ending with a semi-colon ';'.

name ( parameter values );



Children
Use of children allows modules to act as operators applied to any or all of the objects within this module instantiation. In use, operator modules do not end with a semi-colon.

name ( parameter values ){scope of operator} Basicly the children command is used to apply modifications to objects that are focused by a scope: module myModification { rotate([0,45,0]) children; } myModification                // The modification {                               // Begin focus cylinder(10,4,4);             // First child cube([20,2,2], true);         // Second child }                               // End focus

Objects are indexed via integers from 0 to $children-1. OpenSCAD sets $children to the total number of objects within the scope. Objects grouped into a sub scope are treated as one child. See example of separate children below and Scope of variables. Note that,   and empty block statements (including  s) count as   objects, even if no geometry is present (as of v2017.12.23).

children;                        all children children(index);                   value or variable to select one child children([start : step : end]);    select from start to end incremented by step children([start : end]);           step defaults to 1 or -1 children([vector]);                selection of several children

Deprecated child module

Up to release 2013.06 the now deprecated  module was used instead. This can be translated to the new children according to the table:



Examples



Further module examples

 * Objects


 * Operators



Recursive modules
Like functions, modules may contain recursive calls. However, there is no tail-recursion elimination for recursive modules.

The code below generates a crude model of a tree. Each tree branch is itself a modified version of the tree and produced by recursion. Be careful to keep the recursion depth (branching) n below 7 as the number of primitives and the preview time grow exponentially.

Another example of recursive module may be found in Tips and Tricks

Overwriting built-in modules
It is possible to overwrite the built-in modules.

A simple, but pointless example would be: Note that the built-in sphere module can not be called when over written.

A more sensible way to use this language feature is to overwrite the 3D primitives with extruded 2D-primitives. This allows additional to customize the default parameters and to add additional parameters.