Ozapell Basic Learn Programming #6

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.

Example:

FOR X=1 to 10

PRINTIT

NEXT X

SUB PRINTIT

PRINTLINE “HELLO”

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.

Ozapell Basic on Steam

 

Advertisements
Ozapell Basic Learn Programming #6

Ozapell Basic Learn Programming #5

In addition to the FOR NEXT statement(s) discussed previously, there are two more logical flow controlling statements.

The first is WHILE ENDWHILE. If the variable or data provided is TRUE, the code will execute. If it is FALSE, then WHILE will jump to the ENDWHILE and the loop will end.

Example:

VALID=TRUE

WHILE VALID

PRINTLINE “VALID”

VALID=FALSE

ENDWHILE

In this example, the loop will execute only once. If it had been WHILE TRUE, there would have been an infinite loop which could only be stopped by pressing [ESC] on the keyboard.

The second logical flow control statement(s) is REPEAT UNTIL. Unlike WHILE ENDWHILE which may never execute, REPEAT UNTIL will always execute its loop at least once. When the variable or data provided at UNTIL is TRUE, the loop will end.

Second example:

DONE=TRUE

REPEAT

PRINTLINE “OUTPUT”

UNTIL DONE

In this example, the loop will execute only once. If DONE had been assigned FALSE, the loop would be infinite and would require the pressing of [ESC] on the keyboard to end.

Ozapell Basic on Steam

 

Ozapell Basic Learn Programming #5