Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS1051

error return can only be the last statement of a function (or of each branch of a final if / switch).

#What it means

return gives a function its result, and it can only be the last statement: of the function body, or of each branch of a final if / else or switch. Code after a return would never run, and ChartScript has no early return.

#Typical cause

  • Statements after a return.
  • An early return used to leave a function, as in other languages.

#How to fix it

Move the return to the end, and choose between results with if / else (each branch ending in its own return) or the ternary ?:. See Functions.

#Example

ChartScriptError: CS1051
//@chartscript 1
indicator("Body")
function body(o, c) {
    return c - o
    const unused = 0
}
plot(body(open, close))

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Body")
function body(o, c) {
    return c - o
}
plot(body(open, close))
  • CS1037 — Expected => or a block { … } after the parameter list.

The ChartScript documentation is in English for now; translations will follow.