Overbasic – Function

A function is a sequence of OverBasic statements enclosed between Function and EndFunction keywords. A function performs calculations and returns a result. The result is returned to the calling function using the RETURN keyword.

The execution of the function will terminate at the first RETURN encountered, or at the first error/exception NOT correctly handled (see error/exception handling: Try..Catch).

Syntax #

Function <function name> ([ref] <argument1 name> As <argument1 type>, [ref] <argument2 name> As <argument2 type>, ...) As <result type>[()]
    Return <expression>

A function begins with Function keyword and ends with EndFunction keyword.
In the first line of function, immediately after Function keyword, the function name is defined.
After the function name, in the same line, any arguments and the function result type are defined.

In this example, CalculateABS function calculates the absolute value of n1 argument and returns the result:

Function CalculateABS(n1 As Numeric) As Numeric
    If (n1 < 0) Then
        Return -1 * n1
    Return n1

Arguments #

Arguments are (optional) parameters passed to the function to influence its behavior.

An argument is defined using the syntax:

[ref] <argument name> As <argument type>[()]

Each argument is defined by a name and a type. In the example below, value is the argument name and Numeric is the type:

Function DoSomething(value As Numeric) As Numeric


To define an argument as an array, simply add the round brackets immediately after the argument type:

Argument1 As Numeric()

Passing an argument by Reference (ref) #

By default, all arguments of a function are passed by Value, that is, if the argument value is changed within the function, the change will NOT be reflected in the calling function.

By placing ref keyword before the argument name, the argument will be passed to the function by Reference. In this case, by changing the argument value within the function, the change will also be reflected in the calling function:

Function DoSomething(ref value As Numeric) As Numeric
    value = 12
    Return 10 * 5

and in the calling function:

Dim a1 As Numeric = 10
Dim result As Numeric = DoSomething(a1)

After executing DoSomething function, a1 will be 12.

Warnings on Array arguments:

A change to the element value of an array passed as an argument will ALWAYS be reflected in the calling function even if the array is NOT passed by reference (as long as the array has been used at least once before being passed to the function):

Function DoSomething(ref a1 As Numeric()) As Numeric
    a1(5) = 10
    Return 1

and in the calling function:

Dim a1 As Numeric()

Use the array at least once before passing it to the function
a1(2) = 3

Dim result As Numeric = DoSomething(a1)

After executing the DoSomething function, the value of element 5 of a1 will be 10.

Result #

A function must always return a result. Result type is defined in the function first line. In the example below the result type is Numeric:

Function DoSomething() As Numeric

To define function result as an array, simply add the round brackets immediately after the result type:

Function DoSomething() As Numeric()

Main function #

In Overbasic there is a special function called Main. It is the function where the script execution begins.

Function Main()

Main function has no arguments or even a defined result type. In fact, result depends on script type. If the script is an indicator, result type will be Numeric, if Trading System it will be a Boolean. For more information see user-indicators and trading-systems documentation.

The Main function is called automatically by Overcharts every time the script is run. For example, when calculating an indicator, the script is run for each bar in the reference chart. If the indicator reference chart contains 100 bars, the script will run 100 times. Script will also be run on each real time data update.