Please enable JavaScript to view this site.

Testing Project 3

Navigation: Subroutines > MIAMEX

SOSUB (OR CALL) AND RETURN

Scroll Prev Top Next More

The formats are:

 

GOSUB label or line number
CALL label or line number

 

RETURN

 

Calls a subroutine which starts at the line number or label referenced by the GOSUB or CALL statements. The subroutine returns to the statement immediately after the GOSUB or CALL when the RETURN statement is encountered. Executing a RETURN statement without first executing a GOSUB statement results in an error message.

 

Both GOSUB and RETURN are illegal as direct statements. Note that the CALL statement is merely another way of specifying GOSUB for those programmers used to this syntax from other versions of BASIC or other programming languages.

 

It is often the case that you want to perform the same operation at various points within your program. A subroutine is a set of program statements that you may execute more than once simply by including a call for that subroutine within your program at the point where you would like to execute the routine. For example:

 

!        This program contains a subroutine that validates

!        numeric entries to make sure that they are greater

!        than 0 and are less than 100.

 

PRINT "We're going to perform several mathematical "operations."
RE'START:

PRINT "Your entries must be greater than 0 and less than 100."

PRINT : INPUT "Enter two numbers to be added: ",A,B
GOSUB VALIDATE   ! Check to make sure numbers are valid.
IF FLAG = 1 THEN GOTO RE'START    ! Check error flag
PRINT A;" + ";B;" = ";A + B

PRINT : INPUT "Enter two numbers to be subtracted: ",A,B
GOSUB VALIDATE    ! Check to make sure numbers are valid.

IF FLAG = 1 THEN GOTO RE'START
PRINT A;" - ";B;" = ";A - B

PRINT : INPUT "Enter two numbers to be divided: ",A,B
GOSUB VALIDATE    ! Check to make sure numbers are valid.
IF FLAG = 1 THEN GOTO RE'START

PRINT A;" / ";B;" = ";A/B

PRINT : PRINT "Th-th-th-that's all, folks!"
END

VALIDATE:            ! Subroutine to validate the data IF A <= 0 OR B <= 0 &

OR A >= 100 OR B >= 100 & THEN PRINT &

"Error - incorrect number entered in input!" &

: FLAG = 1 & ELSE FLAG = 0

RETURN

 

Remember that & (ampersand) is the symbol for a continuation line.

 

Note that we included an END statement to separate the main program from our subroutine; otherwise, AlphaBASIC executes the VALIDATE subroutine again after printing out the end message, and we would get a "RETURN without GOSUB" error message.

 

Also note that the use of GOSUBs helps to modularize your programs, and thus makes them easier to design and maintain. Even before you completely "flesh out" your programs, you can insert dummy routines that will later contain complete code. For example:

 

!   This program is an example of a dental package.
PRINT "Welcome to the Dr. Plak Rental Dental Package."

PRINT

GOSUB INIT      ! Perform initialization of data files

GOSUB MENU      ! Ask user to pick function from main menu.
GOSUB DAY'END   ! Do End-of-day Processing

GOSUB FINISH'UP ! Finish up, close files, and exit.
END

                     ! The subroutines start here.

 

INIT:

PRINT "This section will initialize files." RETURN

 

MENU:

PRINT "This section will display the main menu and"
PRINT "ask user for selections."

RETURN

 

DAY'END:

PRINT "This section will perform day-end processing."
RETURN

 

 

FINISH'UP:

PRINT "This section will close files and clean up final
PRINT "data."

RETURN

 

You can nest subroutines.  For example:

 

PRINT "Main Program:"

GOSUB OUTER'MOST    ! OUTERMOST calls NEXTMOST and INNERMOST

PRINT "Return from Outermost" END

 

OUTER'MOST:

PRINT " Outermost subroutine"
GOSUB NEXT'MOST

PRINT "    Return from Nextmost"
RETURN

NEXT'MOST:

PRINT " Nextmost subroutine"
GOSUB INNER'MOST

PRINT "    Return from Innermost"
RETURN

INNER'MOST:

PRINT "    Innermost subroutine"
RETURN

 

The program above prints:

 

Main Program:

Outermost subroutine
Nextmost subroutine

Innermost subroutine
Return from Innermost

Return from Nextmost
Return from Outermost

 

It is good programming form to exit a subroutine by using the RETURN statement for that subroutine rather than using a GOTO statement.