SUB name (optional parameters)

SUB name       ' a SUB without parameters


SUB name (parameter list) 
  parameter list = parameter [, parameter list]  
                   |   PARAMARRAY paramarray 
  parameter  =  [BYVAL] paramname [AS INTEGER]
                   |  [BYVAL] paramname(size) AS STRING
                   |   BYREF paramname AS STRING 
                   |   BYREF paramname  [AS INTEGER] 


GOSUB goes to a label.  , but can also go to a defined SUB procedure.

The SUB.. ENDSUB construct allows for a second scope of variables.  Scope meaning the region in which code can see a set of labels.  ARMbasic has a global scope and a local scope for any variable declared with DIM inside an SUB. Local scope variables will be only accessible from within that SUB procedure (the local scope).

Simple parameters are assumed to be called BYVAL if not specified.  In BYVAL calls, a copy of the parameter is passed to the SUB procedure.  Integer or string parameters may be called BYREF which means a pointer to the integer/string is passed, and changes to that integer/string can be made by code inside the SUB procedure. STRINGs may be passed either BYVAL or BYREF, but arrays must be passed BYREF.

Code labels for goto/gosub declared within the SUB procedure are also in the local scope. Call to global labels are allowed within a SUB .. ENDSUB, but that global label must be defined BEFORE the SUB ... ENDSUB.

SUB without any parameters can be called with or without (), an empty parameter list

Recursive calls with parameters or local variables are not supported.  And ENDSUB or END SUB syntax are allowed.

Program structure:

SUB procedures should be arranged ahead of the MAIN: body of code.  In many cases they will be part of #include files at the beginning of the user ARMbasic code. If SUBs are located at the start of a program a MAIN: must be used.

SUB procedures can access global variables that have been declared before the SUB, this declaration can either be implicit or use a DIM.

An implied RETURN is compiled at the ENDSUB, but code may also return to the caller with RETURN

SUBs must be defined before they are called.


SUB sayHello
    DIM I as INTEGER      ' this variable is local to the sayHello SUB procedure
    FOR I=1 to 3
        PRINT "Hello"
    NEXT I

I = 55
PRINT I                        ' will display 55
GOSUB sayHello

PRINT I                        ' will still display 55, as this is the global I, different from sayHello local I

Differences from other BASICs

See also