Subroutines

<< Click to Display Table of Contents >>

Home  mIoTa BASIC > mIoTa BASIC Language Reference > Statements >

Subroutines

Subroutines are subprograms (self-contained statement blocks) which perform a certain task that may be required in multiple places within a program. Using Subroutines allows blocks of code that may have been repeated though a program to be simply called from where ever it is required, reducing the size of the code and simplifying the look of the program in terms of readability.

 

 

Declaring a Subroutine

 

Subroutines are declared using the SUB and ENDSUB commands, like this:

 

Sub my_sub;

// code line 1

// code line 2

// code line ..

EndSub;

 

Subroutines can be defined anywhere in a program and called from anywhere else in the program, but must be defined after the end of an Event Code block. An Event Code block can have no code but can still have Subroutine declarations.

 

For example ..

 

// end of Event Code block

 

Sub DoFormula; // my subroutine to multiply by 1000 and add 25

  u2 = (u1 * 1000) + 25;

EndSub;

 

Sub ReverseFormula; // my subroutine to subtract 25 then divide by 1000 (reverse of DoFormula)

  u2 = (u1 - 25) / 1000;

EndSub;

 

 

Calling Subroutines

Subroutines are called using the GOSUB keyword, like this:

 

Gosub my_sub;

 

Execution returns to just after the calling GOSUB when the ENDSUB command is encountered at the end of the subroutine.

 

 

Exiting a Subroutine

 

You can if needed jump back out of a subroutine at any point using the EXIT command. This effectively jumps to the end of the Subroutine and returns to the calling GOSUB.

 

 

© 2018 Swintechnics