func, then call it as often as you like. An arrow body (=>) is enough for one expression.
A body with several statements
Use a braced block andreturn. 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 writtenarray<float>.
What a function cannot do
- Draw with the plot family.
plot,plotshape,bgcolor,filland the rest must be called at the top level. Return the value and plot it there. Drawings (label.new,line.new,box.new) andalertare 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.
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.