Introduction to Procedures

Top  Previous  Next

A procedure is a named piece of code, which performs a designated task, and can be called (used) by other parts of the program. In Tibbo Basic, there are two types of procedures:

Function Procedures

A function is defined using the Function Statement. Functions can optionally have one or several arguments. Functions always return a single value. They can, however, change the value of the arguments passed to them using ByRef and thus indirectly return more than one value. This would be an example of a function:

 

 

function multiply(a as integer, b as integer) as integer

       multiply = a * b

end function

 

 

Note how the function above returns a value: via a local variable with the same name as the function itself. Such a variable is automatically created by the compiler for each function.

Sub Procedures

Sub is short for subroutine; just like a function, a sub procedure can optionally accept one or more arguments. However, unlike functions, sub procedures do not return a value. It is defined using the Sub Statement. This would be an example of a sub:

 

 

dim a(10) as byte ' a is a global variable -- outside the scope of the function.

 

sub init_array

       dim i as integer

       for i = 0 to 9

               a(i) = 0 ' the global variable gets changed.

       next i

end sub

 

 

Subs change the value of the arguments passed to them using ByRef and thus indirectly return a value, or even several values. Of course, they may also change the value of global variables.

Event handlers are like subs

Event handlers defined in the platform work exactly like sub procedures. Event handler subs can accept arguments. Event handlers can never be function procedures as each function has to return a value and the event handler has nobody to return this value to.

Declaring Procedures

Usually, a procedure is first defined by the function or sub statements and then used in code; however, at times, functions can reside in a different compilation unit. In such a case, you must use the public modifier when defining this function, and use the declare statement to let the compiler know that the function exists.

For example, let us say this is the file utility_functions.tbs:

 

 

public function multiply(a as integer, b as integer) as integer ' the value returns by this function is an integer

       multiply = a * b

end function

 

 

And this is the file program.tbs:

 

 

declare function multiply(a as integer, b as integer) as integer ' declaring just the name isn't enough. Include also the arguments and the types.

dim i as integer

i = multiply(3, 7)

 

 

Declare statements are usually used within header files which are then included into compilation units. Also, if for some reason you would attempt to use a procedure in a single compilation unit before defining it, you will have to use a declare statement to let the compiler know that it exists. For example:

 

 

declare function multiply(a as integer, b as integer) as integer

dim i as integer

i = multiply(3, 7)

 

...

 

function multiply(a as integer, b as integer) as integer ' now this function doesn't have to be public.

       multiply = a * b

end function

 

 

Event handler subs require no declaration as they are already declared in your device's platform.

No Recursion

One thing you have to know is that procedures cannot call themselves. Also, two procedures cannot call each other. This is due to TiOS not using dynamic memory allocation. Such allocation would create a serious overhead for the system, and would drastically slow everything down -- not just recursive procedures. For more information, see Memory Allocation for Procedures.