Lipi Reference Lipi

Keywords

def

This keyword is used to define a function as a method, allowing it to be called using dot notation by appending its name to a variable matching the type of its first parameter while omitting that parameter in the call. Alternatively, methods can also be invoked like regular user-defined functions, in which case the first parameter must be explicitly provided as an argument. Additionally, the first parameter in a method declaration must have an explicitly defined type.

Syntax

                def <function_name>(<data_type> <param_name> [= <default_value>], ...) {
	<function_body>
}
		
func <function_name>(<data_type> <param_name> [= <default_value>], ...) => <expression>
              

Example


          indicator("Integer Method Example", overlay=true)
// Define a method for the int type
def square(int x) {
 return x * x
}
// Define an integer variable
myNumber = 4
// Invoke the method using dot notation
squaredValue = myNumber.square()
// Plot the squared value
plot(squaredValue, title="Squared Value", color=color.blue)