Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS2502

error Functions cannot be declared inside other functions.

#What it means

Functions must be declared at the top level of the script, not inside another function (or inside an if or a loop).

#Typical cause

  • A helper function written inside the function that uses it.

#How to fix it

Move the inner function out to the top level. It can still be called from the other function.

#Example

ChartScriptError: CS2502
//@chartscript 1
indicator("Helpers")
function outer(x) {
    const inner = (y) => y * 2
    return inner(x)
}
plot(outer(close))

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Helpers")
const inner = (y) => y * 2
const outer = (x) => inner(x)
plot(outer(close))
  • CS2501 — name calls itself (through path); recursion is not allowed.
  • CS3007 — name can only be used at the top level of the script, not inside a condition, loop or function.

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