Overbasic – Return

RETURN keyword returns the result of a function. The result type (Numeric, Boolean, String etc.) must match the type declared in the first line of function.

  • RETURN is mandatory in every Overbasic function! Without RETURN the function will not return any results.
  • In addition to returning the result, RETURN ends the function execution.

Syntax #

Return <expression>

<expression> MUST be of the same type declared in the first line of function.

Examples

Function DoSomething() As Numeric
    Return 10 * 5 'returns a numeric
EndFunction

Function DoSomething() As Numeric
    Dim i As Numeric = 0
    i = 10 * 5
    Return i 'returns a numeric
EndFunction

Function DoSomething() As Numeric()
    Dim a As Numeric()
    a(1) = 10 * 5
    Return a 'returns a numeric array
EndFunction

Function DoSomething() As Boolean
    Dim b As Boolean = False
    If (Close > Open) Then
        b = True
    EndIf
    Return b 'returns a boolean
EndFunction

Function DoSomething() As String
    Return "Hello!" 'returns a string
EndFunction

RETURN in the 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. Result depends on script type.

If the script is an indicator:

  • result is ALWAYS Numeric
  • RETURN in the MAIN function can return up to a maximum of 10 results at the same time which will graphically correspond to 10 different curves.
  • when RETURN returns only 1 result, the identifying name is NOT required
  • when RETURN returns more than one result, the identifying name is required
Return <expression1> As <name1>, <expression2> As <name2>, ... , <expression10> As <name10>

Examples

Function Main()   
    Dim v1 As Numeric = (Close + Open) / 2

    Return v1
EndFunction

Function Main()
    Dim v1 As Numeric = Open(-1)
    Dim v2 As Numeric = Close(-1)
    Dim v3 As Numeric = (Close + Open) / 2

    Return v1 As PrevOpen, v2 As PrevClose, v3 As CurrentMiddle
EndFunction

If the script is a Trading System:

  • result is ALWAYS 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.

RETURN exits the function #

RETURN, in addition to returning function result, ends function execution. In other words, any statements after RETURN keyword will NOT be executed.

Function DoSomething() As Boolean
    Dim b As Boolean = False
    If (Close > Open) Then
        b = True
    EndIf
    
    Return b 'returns a boolean
    
    'Any statements after RETURN keyword will NOT be executed
    If (High > High(-1)) Then
        b = True
    EndIf
EndFunction

On Events:

Events are special procedures called automatically by the Overbasic script when specific events occur. In events, RETURN keyword returns no results. However, it can be used to exit the procedure.

Event OnProcess()
    ...
    <statements>
    ...
    Return
EndEvent