Skip to main content
Declare the calculation once with func, then call it as often as you like. An arrow body (=>) is enough for one expression.
Every parameter states its data type. A function must be declared above the line that calls it.

A body with several statements

Use a braced block and return. A parameter may have a default, a literal or a built-in variable such as close; a caller may then leave that argument out.
func and no keyword at all declare an ordinary function (func f(float a) => a and f(float a) => a are the same). def declares a function that can also be called on its first argument, value.f(), and needs at least one parameter. To return several values, see How to return more than one value from a function.

An array as a parameter

An array is shared, not copied, so the function reads the caller’s array and may change its contents. The type is written array<float>.

What a function cannot do

  • Draw with the plot family. plot, plotshape, bgcolor, fill and the rest must be called at the top level. Return the value and plot it there. Drawings (label.new, line.new, box.new) and alert are allowed inside a function.
  • Reassign a global variable. It may read globals, and change the contents of a global array or the fields of a global object.
The compiler says: Cannot modify global variable total in function. Return the new value and assign it at the top level, or keep the total in a static variable at the top level. A plot inside a function is refused with plot() must be called in the global scope. See Your own functions.