> ## 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 return more than one value from a function

> Returning a tuple from an arrow body or a braced body, unpacking it with [a, b] = f(), and the built-in functions that return tuples the same way.

Return a bracketed list, a tuple, and unpack it in one statement with the names in brackets on the left of `=`. The names take the
values in the order they were returned, and the count must match.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Band around the average", overlay=true)
func band(float centre, float width) => [centre - width, centre + width]
average = talib.sma(close, 20)
[lower, upper] = band(average, talib.atr(14))
plot(average, title="Average", color=color.gray)
plot(lower, title="Lower", color=color.blue)
plot(upper, title="Upper", color=color.blue)
```

A braced body returns a tuple with `return [a, b, c]`. The elements may have different types.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Tuple from a block", overlay=true)
func range3(float a, float b) {
    mid = (a + b) / 2
    return [a, mid, b]
}
[lo, mid, hi] = range3(low, high)
plot(mid, title="Mid")
[macdLine, signalLine, hist] = talib.macd(close, 12, 26, 9)
plot(hist, title="Histogram")
```

## Built-in tuples

`talib.macd(source, fastlen, slowlen, siglen)` returns `[macd, signal, histogram]`. `talib.bb(source, length, mult)`,
`talib.kc(source, length, mult)` and `talib.dc(length)` return `[middle, upper, lower]`. `talib.dmi(diLength, adxSmoothing)` returns
`[plusDI, minusDI, adx]` and `talib.supertrend(multiplier, atrLength)` returns `[supertrend, direction]`. Unpack them as above.

For many values of one type, return an `array<float>` (see [Arrays](https://gocharting.com/docs/scripting/language/arrays.md)); for several named values, a
`type` of your own (see [Custom types and enums](https://gocharting.com/docs/scripting/language/custom-types.md)).

## Reassign and the rules

* `[a, b] := f()` assigns to names that already exist; `[a, b] = f()` declares them, and repeating it for existing names is an error.
* A tuple cannot be stored in one variable, and `[a, b]` is only valid as a function result or an argument, not on the right of `=`.
* Tuples do not nest.

## The mistakes to avoid

> [!WARNING]
>
> Storing the tuple in one name. The compiler says: Cannot assign a tuple value `[series float, series float]` to variable `t`.
>
> ```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
> //@lipi=2
> indicator("Tuple in one variable", overlay=true)
> func band(float c, float w) => [c - w, c + w]
> t = band(close, 1)
> plot(close)
> ```

Unpacking the wrong number of names gives: Number of variables on the left side of the assignment operator does not match the number of
elements in the tuple on the right side. Expected: 2, Got: 3. And `[1]` after the call is the history operator, which a tuple
does not accept: History Indexing not allowed on tuple expression. Unpack first, then index the name you unpacked.
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.