Home » Pinescript Syntax » Understanding the “Void” Type in Pine Script

Understanding the “Void” Type in Pine Script

Photo of author
Published on

This article aims to shed light on the void type, its purpose, and its usage in Pine Script version 5.

What is the “Void” Type?

The void type in Pine Script is used to signify that a function does not return a usable value. This is common in many programming languages, where functions perform actions or cause side effects without actually producing a return value. In Pine Script, an example of a function that returns void is alert(). This function triggers an alert event but does not return any value that can be used in further expressions or assigned to variables.

Key Characteristics

  • Side Effects Only: Functions returning void are designed solely for their side effects, such as displaying alerts, plotting on the chart, or executing orders in strategy scripts.
  • Non-assignable: Since void results are essentially non-values, they cannot be assigned to variables or used in expressions.

Usage in Pine Script

Understanding when and how to use void functions is crucial for effective Pine Script programming. Here’s a closer look at the practical application of void type functions:

Example: The alert() Function

//@version=5
indicator("My Script", overlay=true)
alertcondition(condition=true, title="Alert Title", message="Alert Message")

In this simple example, the alertcondition function is used to define a condition under which an alert will be triggered. While alertcondition itself does not return a value, it causes a side effect by making the alert available for the user to set up in the TradingView interface.

Important Considerations

  • No void Keyword: Unlike some programming languages, Pine Script does not have a void keyword. This is because you cannot declare a variable to be of type void, reflecting the language’s design around trading-specific functionalities rather than general-purpose programming.
  • Script Limitations: Scripts in Pine Script cannot directly use the results of void functions in expressions. This limitation ensures clarity in script logic and prevents misuse of non-value returning functions.

Summary

  • Side-Effect Functions: void type functions are essential for operations that affect the script or chart without returning a value.
  • Usage Restrictions: The results of void functions cannot be used in expressions or assigned to variables, ensuring scripts remain clear and logical.
  • No Explicit Declaration: Pine Script does not use a void keyword, as variables cannot be declared with this type.

Understanding the void type and its associated functions is crucial for developing efficient and effective scripts in Pine Script. By recognizing the purpose and limitations of void functions, developers can better structure their scripts for optimal performance and clarity.

Leave a Comment