Logic Basic tutorial for beginners
Subroutines, Functions and Timers

Subroutines and functions are features that allow the programmer to create routines (code snippets that perform a particular function), and can be executed at any time within the main program. The difference between subroutine and function is that the latter can return values, while the subroutine simply executes a piece of code without returning values.

To create a subroutine or function, you first must declare them, and this should always be done after the command EndProgram or EndWindow. If they are placed within one of the code extensions is not necessary to inform the command EndProgram in the main code, because the Logic Basic enters this command automatically at the end of the main code when the program runs.

Subroutines

To declare a subroutine, you must type the statement Sub, then the name of the subroutine, and open-close parenthesis, as shown below:

Sub Test()
  'Code of the subroutine
EndSub

Note that for every command Sub, you must write your respective command EndSub, which indicates where the subroutine ends.

After you have declared the subroutine, you should write your code, which must be between the command Sub and EndSub, for example:

Sub Test()
  Write "Hello martian!"
EndSub

This subroutine named Test, will write the phrase "Hello martian!" in the active window, to run it, write your name followed by open-close parenthesis at any point in the program, eg:

Test()
Write "End of the program!"
EndProgram

Sub Test()
  Write "Hello martian!"
EndSub

In the above program the subroutine was called in the first line, then the Logic Basic runs their code, writing Hello martian! in the active window, then returned the program execution to the next line after the subroutine and wrote phrase End of the program.
Functions

A function can take arguments (parameters) and also return values ​​or strings, while subroutines do not have these resources.

To declare a function, you must write the statement Function, then open parenthesis and write the names of the arguments and their type (if there are arguments), then closes the parenthesis and write the type of variable it returns (if it will return values ​​or strings), eg:

Function Example(Argument1 Type, Argument2 Type) Type

For example, let's create a function to add two integer values ​​to be passed as arguments, and returns the sum:

Function Sum(X Integer, Y Integer) Integer
Variable Ret Integer

Ret = X + Y

Return Ret
EndFunction

Note that in the above example for each declared function you must declare their respective Command EndFunction that indicates where the function ends. Note also that we use a command called "Return", which returns a value (or string).

An important detail is that the returned value should be of the same declared type of the function, which in our example is of type Integer.

When we declare the arguments and their types, for each function call, the Logic Basic will create variables for these arguments, which will be destroyed after the end of the execution of the function, ie they are local variables, and the name of these arguments can be equal to the name of the arguments of other functions without causing conflicts.

When performing a function, you must assign your return to a variable, or pass it as an argument to another command or function of the Logic Basic, and the arguments must be informed in parenthesis, and in the same order they are declared, for example:

Variable T Integer

T = Sum(5, 2)

Write "The value of the sum of 5 + 2 is equals to ", T

EndProgram

Function Sum(X Integer, Y Integer) Integer
Variable Ret Integer

Ret = X + Y

Return Ret
EndFunction
Timers

Timers are subroutines that perform its code at certain intervals of time, without the need for you to call them in the program code. Are useful to monitor events that occur in the program in real time, for example, check what is being typed in a text box, or even create a watch that is active while other routines of the program are executed.

To declare a Timer, you should write the statement Sub, then the word Timer, sub-line and the timer name, then type the value of the interval (in hundredths of a second) in parenthesis, for example:

Sub Timer_Test(50)

There is also a command with the same name (Timer), which has two methods: Activate and Off. In method Activate you should pass as argument the value of the interval in hundredths of a second, for example:

Timer Test.Off
Timer Test.Activate, 20

In the above example the timer Test was disabled and then enabled again with a new value for the interval (20 hundredths of a second).


Back to index      Next topic