> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to reuse a calculation with your own function

> Declaring a function once and calling it twice, the arrow and braced body forms, parameter defaults, and what a function cannot do.

Declare the calculation once with `func`, then call it as often as you like. An arrow body (`=>`) is enough for one expression.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Two spreads", overlay=false)
func spread(float a, float b) => a - b
plot(spread(high, low), title="Bar range")
plot(spread(close, open), title="Body")
```

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.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Weighted close", overlay=true)
func weighted(float a, float b, float weight = 2.0) {
    return (a * weight + b) / (weight + 1)
}
plot(weighted(close, close[1]), title="Weighted")
plot(weighted(close, close[1], 1.0), title="Plain mean")
```

`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](https://gocharting.com/docs/scripting/how-to/return-more-than-one-value-from-a-function.md).

## 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>`.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Mean of the last five closes", overlay=true)
static array<float> window = array.new<float>(0)
window.push(close)
if window.length() > 5 {
    window.shift()
}
func mean(array<float> a) {
    float s = 0
    for x in a {
        s += x
    }
    return s / a.length()
}
plot(mean(window), title="Mean of 5")
```

## 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.

```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Function reassigns a global")
float total = 0
func add(float x) {
    total := total + x
    return total
}
plot(add(close))
```

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](https://gocharting.com/docs/scripting/language/custom-functions.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.