User-Defined Functions
Define a function once and reuse it on subsequent lines. Function bodies support the full expression syntax — operators, units, currencies, constants, and built-in functions.
Defining a function
Section titled “Defining a function”A definition has the form name(param) = body. The line that contains
the definition produces no result; the function is registered for use on
later lines.
| Calculation | Result |
|---|---|
f(x) = 2*x + 1 | |
f(5) | 11 |
f(10) | 21 |
Multiple arguments
Section titled “Multiple arguments”Functions accept any number of parameters separated by commas.
| Calculation | Result |
|---|---|
area(w, h) = w * h | |
area(3, 4) | 12 |
| |
vol(l, w, h) = l * w * h | |
vol(2, 3, 4) | 24 |
Naming rules
Section titled “Naming rules”- Names start with a letter and may contain letters, digits, and underscores. No spaces, no leading digit.
- At least one parameter is required. For zero-argument values, use a
variable instead (
pi_sq = pi * pi). - Parameter names within a single definition must be distinct —
f(x, x) = …is rejected.
Bodies can use the full expression language
Section titled “Bodies can use the full expression language”Function bodies parse with the same grammar as ordinary lines, so they can use built-in functions, constants, units, currencies, and operators.
| Calculation | Result |
|---|---|
hyp(a, b) = sqrt(a*a + b*b) | |
hyp(3, 4) | 5 |
| |
circle(r) = pi * r * r | |
circle(2) | 12.57 |
| |
withTax(x) = x * 1.10 | |
withTax(100) | 110.00 |
A numeric parameter can be wrapped in a currency or unit using either
in <unit> or bare adjacency — useful when the function is meant to
return a typed value:
| Calculation | Result |
|---|---|
inEuros(x) = x in EUR | |
inEuros(20) | €20 |
| |
distance(x) = x km | |
distance(5) | 5 km |
Conversions inside bodies compose naturally, so a currency or unit amount can be re-expressed in a different one:
The currency result below uses an illustrative rate of USD 1 = EUR 0.8. Live currency results vary with the app’s current rate.
| Calculation | Result |
|---|---|
inDollars(x) = x EUR in USD | |
inDollars(100) | $125 |
| |
inCm(x) = x m in cm | |
inCm(5) | 500 cm |
Calls compose with arithmetic and with each other:
| Calculation | Result |
|---|---|
double(x) = 2 * x | |
double(5) + 1 | 11 |
double(double(5)) | 20 |
| |
square(x) = x * x | |
square(2 + 3) | 25 |
Restrictions
Section titled “Restrictions”A few intentional limits keep function calls fast and predictable:
- Definitions must come before calls. A line can only reference a function defined on an earlier line.
- No recursion. A function body cannot call itself, and two functions cannot call each other (mutual recursion). Recursive calls resolve to nothing rather than hanging.
- Bodies are isolated from line-scoped operations. Inside a body,
line1,previous, andtotaldo not see the surrounding document —previousandtotalresolve to0, andline<N>resolves toNaN. - Outer variables don’t leak into bodies. Only the function’s own
parameters are in scope; you cannot reference
yinside a body unlessyis one of the parameters. - Built-in names always win. Defining
sum(x) = x + 1does not override the built-insum(…)— calls go to the built-in. Pick a different name to avoid the clash.