User Tools

Site Tools



Sub and Function

For subroutines, you can either use Sub or Function. Function can return a value, but Sub can't.

Sub SubName (Param1 As DataType [,ParamX As DataType][,…])
    [Exit sub] ' Exit during sub-routine
End Sub
Function FunctionName (Param1 As DataType [,…])[As ReturnDataType]
    [Exit Function] ' Exit during sub-routine
End Function

To return values using Function, simply store the final value as the name of the function as shown below:

Function ADD_VALUE(B As Byte) As Byte
    ADD_VALUE = B + 1 ' Return B+1.
End Function

Global and Local Variables

When you declare variables inside a subroutine or a function, it is considered a “Local” variable. Local variables are created when the subroutine or function is called, and removed when the subroutine or function exits. This means that local variables will temporarily allocate data memory for the duration of the call. Local variables may only be referred to or used inside the subroutine or function in which they were declared.

On the other hand, global variables may be used anywhere in your program.

Dim A As Integer          ' Declare A as Global Variable
    A = A + 1
    Debug Dp(A),CR        ' Display A on Debug screen
    DELAYTIME             ' Call Sub DELAYTIME
    GoTo LOOP1
End                       ' End of Main Program
    Dim K As Integer      ' Declare K as Local Variable
    For K=0 To 10
End Sub

In the program above, A is declared as a global variable and K is declared as a local variable. A can be used anywhere in the program but K can only be used inside the subroutine DELAYTIME.

Arrays cannot be declared as local variables. Arrays must be declared as global variables.

Calling Subroutines

Once the subroutine is created, they can be used like any other statement. For a subroutine, you do not need parenthesis around the parameters. Use commas to separate multiple parameters.

The example below shows how this is done:

DELAYTIME 100 ' Call subroutine
Sub DELAYTIME(DL As Integer)
    Dim K As Integer ' Declare K as Local Variable
    For K=0 To DL
End Sub

For a function, you need parenthesis around the parameters. Parenthesis are required even when there are no parameters.

Dim K As Integer
K = SUMAB(100,200) ' Call subroutine and store return value in K
Debug Dec K,cr
    SUMAB = A + B
End Function

Subroutine Position

Subroutines must be created after the main program. To do this, simply put End at the end of your main program as shown below. End is only required if you have subroutines

Dim A As Integer
    A = A + 1
    Debug DP(A),CR
    Goto Loop1
    End                   ' End of main program
    Dim K As Integer
    For K=0 To 10
End Sub

Subroutines and functions are created after the End statement. Gosub subroutines must be within the main program like shown below:

The End statement is used to differentiate between the BASIC main program and the program's subroutines. The END statement in Ladder Logic is used to indicate the final Ladder Logic rung.

Subroutine Parameters and Return Values

Functions may use any data type, except arrays, as parameters and return values:

Dim A(10) As Integer
Function ABC(A AS Single) as Single                  ' Return Single value
End Function
Function ABC(A AS String * 12) as String *12         ' Return String value
End Function
Function ABC(A AS long)                 ' Long value as a parameter
End Function                            ' When return value is not declared,Long
                                        ' will be used as return value.

Arrays cannot be used as parameters. The following is not allowed.

Function ARRAYUSING(A(10) AS Integer)    ' Arrays may not be used as parameters.
End Function

But you may use one element of an array as a parameter:

Dim b(10) as Integer
K = ARRAYUSING(b(10))            ' Use 10th element of array b as a parameter.
Function ARRAYUSING(A AS Integer) as Integer
End Function

All subroutine parameters are passed by value, not by reference. If the parameter value is changed within a subroutine, it will not affect the variable passed to the subroutine.

Dim A As Integer
Dim K As Integer
A = 100
Debug Dec? A, Dec? K,CR ' A is 100 and K is 110
Sub ADDATEN(V As Integer)
    V = V + 10 ' A does not change when V is changed.
End Sub

In contrast, some languages allow values to be passed by reference in which the actual memory address is passed to the subroutine. Cubloc BASIC only supports passing by value.

Too many characters in one line?

If you run out of room, you can use an underscore character (_) to go to the next line as shown here:



Use an apostrophe/single-quote (') to add comments. Comments are discarded during at compile time, and will not consume any program memory.

ADD_VALUE = B + 1 ' Add 1 to B.(Comment)

Nested Subroutines

Nested subroutines are supported in Cubloc.

A=Floor(SQR(F)) ' Do Floor() on Sqr(F).


Colons cannot be used to append commands in Cubloc BASIC, as is possible in some other languages.

A=1: B=1 : C=1 ' Incorrect.
A=1 ' Correct.

Go CUBLOC home

cubloc/sub_and_function/index.txt · Last modified: 2016/04/14 11:07 (external edit)