Sub declaration

A sub-declaration within a HBasic program declares the body of a subroutine. Subroutines may be called in another statement or may be used as event handler for class or component events. See connecting to events to understand how to change a subroutine into an event handler.

<Sub-declaration> ::=
    <Sub-header>
       <statement list>
    <End-Sub-token>

<Sub-header> ::=
    Sub
<identifier> ( <parameter-list> ) As <returntype> [ Handles <identifier> ]

<returntype> ::= <simpletype>

<parameter-list> ::= <parameter-desc> { , <parameter-desc> }
<parameter-desc> ::= [ByVal | ByRef ] <identifier> As <typedesc>

<Sub-header-token> ::= Sub | Method | Function
<End-sub-token> ::= End Sub | End Method | End Function

Return value of a subroutine

If the subroutine definition has a return value you can assign the return value to the subroutine name within the body of the subroutine.

Example:

Sub test() As Integer
   test = 1234
End Sub
In this example the statement test = 1234 will set the return value of the subroutine to the value 1234.


Subroutine call

<function-call> ::= sub-name ( <expr-list> )

where sub_name is either the name of a subroutine that has been defined somewhere else in your HBasic program. or the name of a global method that has been defined within a package that's currently loaded in your project.



Return statement


<Return statement>



Connecting to events

A subroutine declaration becomes an event handler if one of the two following conditions is true:

1) Immediatly behind the parameter list of the subroutine is a Handles structure
        Sub identifier ( parameter-list ) Handles compname.eventname
or     Sub identifier ( parameter-list ) Handles classname.eventname
2) The name of the subroutine is in the form compname_eventname or classname_eventname

The identifier eventname therefore has to be the name of an event that has been defined within a class or the name of  an event from the list of events of a component or qtc class.

Special implemented event handler is form_load. This will be called when opening the form.