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
returnused 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
//@chartscript 1
indicator("Body")
function body(o, c) {
return c - o
const unused = 0
}
plot(body(open, close))Fixed
//@chartscript 1
indicator("Body")
function body(o, c) {
return c - o
}
plot(body(open, close))#Related
- CS1037 — Expected => or a block { … } after the parameter list.