A subroutine is a section of code that can be executed once or many times. The code in a subroutine is encapsulated. If properly designed, a subroutine could be used by other programs after its first development. Make code reusable to save time.
In Ozapell Basic, a new subroutine is declared using the SUB statement.
The main section of an Ozapell Basic program is always at the top of the source code. All subroutines declared must be after the main section. The compiler determines that the main section has ended when it encounters the first SUB declaration or the end of the source code.
FOR X=1 to 10
In this example, the subroutine PRINTIT is declared. The compiler determines the end of the subroutine when it either encounters the next SUB declaration or reaches the end of the source code.
PRINTIT is called by writing its name alone as a line of code. Each time PRINTIT is called, the code will execute. In this example, the text string “HELLO” will be assigned to the variable TEXT and the built-in display routine PRINTLINE will show HELLO on the screen.
PRINTLINE is a built-in subroutine that uses the variable TEXT. New subroutines can use any variable available as long as the variable is declared before calling the subroutine.
INPUT is a built-in subroutine returning a key pressed in the RESULT variable. New subroutines can declare and return any variables necessary.