User-Defined Functions
User-Defined Functions in Pine Script
Introduction
User-defined functions, crafted by the user in contrast to the built-in functions available in Pine Script, offer the ability to define custom calculations needed frequently or to segregate a portion of calculations from the script's main section. User-defined functions can essentially extend the capabilities of Pine Script when built-in functions are not suitable for specific needs.
User-defined functions can be written in two forms:
Single-line functions: Ideal for simple calculations.
Multi-line functions: For complex calculations requiring multiple lines.
Moreover, these functions can be positioned in two places:
Within the script where they're being used, if they're specific to that script only.
In a Pine Script library, making them reusable in other scripts without code replication. Distinct requirements exist for library functions, explained in the Pine Script Libraries section.
Regardless of whether they're written in a single line or multiple lines, user-defined functions share some common characteristics:
They cannot be nested; all functions are defined in the global scope of the script.
They don't support recursion; a function cannot call itself from within its code.
The return type is determined automatically, based on the type of arguments used in each function call.
Single-Line Functions
Single-line functions, often ideal for simple computations, follow this structure:
Here's an example of a single-line function that adds two numbers:
Once defined, you can call this function using various argument types:
Multi-Line Functions
Multi-line functions in Pine Script are handy for complex calculations, and they follow this structure:
The body of a multi-line function contains several statements. Each statement must be placed on a separate line and be preceded by 1 indentation (4 spaces or 1 tab). The last statement of the function's body (either an expression or a declared variable) will be the result of the function's call.
Here's an example of a multi-line function:
Scope in Scripts
Variables declared outside a function belong to the global scope, as do user-declared and built-in functions and variables. Each function has its local scope, including all the variables declared within the function and the function's arguments.
Functions cannot access variables from other functions' local scopes, but they can reference any global scope variable or function (except for recursive calls).
Functions Returning Multiple Results
While most functions return a single result, Pine Script allows functions to return multiple results as a list or tuple-like result:
To call such functions, use the following syntax:
Limitations
Despite their versatility, user-defined functions in Pine Script have some limitations:
All functions are defined in the scriptโs global scope and cannot be nested.
Functions do not support recursion.
The type of the value returned by a function depends on the type of arguments used in each function call.
Last updated