FUNCTION
Defines a function of your own: a value worked out from its arguments, or geometry made from them, or both.
Syntax
FUNCTION NAME(parameter,parameter,…) = expression
FUNCTION NAME(parameter,parameter,…) BEGIN … RETURN expression … END
| Argument | Meaning |
|---|---|
| NAME | The function's name: a name, not a word of the language (SIN, LINE …). One with a built-in function's name (TRIANGULATE …) is used instead of it. |
| parameter | The names the arguments it is called with have inside it (none: FUNCTION NAME()). |
| expression | The function's value: an expression of its parameters (and the drawing's variables). |
Examples
FUNCTION HYPOT(A, B) = SQRT(A * A + B * B)
DIAGONAL = HYPOT(L, W)

DIAGONAL follows the box as L and W change.
FUNCTION FACTORIAL(N) BEGIN
IF N <= 1 THEN BEGIN RETURN 1 END
RETURN N * FACTORIAL(N - 1)
END
PRINT FACTORIAL(6)
FUNCTION SLOT(C, LENGTH, R) BEGIN
P1 = C + [-LENGTH / 2, -R] ; P2 = C + [LENGTH / 2, -R]
P3 = C + [LENGTH / 2, R] ; P4 = C + [-LENGTH / 2, R]
P5 = C + [LENGTH / 2, 0] ; P6 = C + [-LENGTH / 2, 0]
LINE P1, P2
ARC P2, P5, P3, TRUE
LINE P3, P4
ARC P4, P6, P1, TRUE
END
FOR I = 1 TO 3 BEGIN
CALL SLOT([I * 120, 200], 60, 10)
END
Three slots, made by CALL.
Notes
A function is called by its name, with its arguments in brackets, anywhere an expression goes: HYPOT(3, 4). Its
value is what it RETURNs. A function that makes geometry is drawn with CALL.
A function is like a part: it has its own variables and points. Its parameters, and the
variables and points it sets, are its own: P1 in SLOT above isn't the drawing's P1. Its points start with P0 at its
origin, [0,0]. It sees the drawing's variables (L, W …) as they are when it is called, and the other functions.
A function can call itself, up to 100 calls deep. A function is looked up when it is called, so it can be used before it is defined. Defining a function again with the same name replaces it.
Functions are saved at the start of the drawing file.
A function can also be written in JavaScript: see JavaScript functions.
See also: Blocks and control flow, CALL, JavaScript functions.