Acorn Computers BBC Micro Model B w/8271 — Acorn BBC Micro Model B w/8271 User Guide Manual PDF

236 GOSUB go to a subroutine Purpose Often a group of lines in a program needs to be used in a number of different places within the main program. Instead of repeating the same piece of program several times you can make this small sub-section into a separate subroutine. This subroutine can then be ‘called’ from a number of different places in the main program by means of the statement GOSUB . The end of a subroutine is indicated by the word RETURN . This causes the program to return to the statement after the GOSUB statement. Beware of a subroutine calling itself too many times: a depth of 26 subroutines is the maximum that is allowed.

As with GOTO , it is possible to GOSUB to a calculated line number. The same cautions that apply to GOTO apply to GOSUB in this case. Example 1020 GOSUB 4000 Description A statement used to call a section of program as a subroutine. One subroutine may call another subroutine (or itself) up to a maximum nested depth of 26. Syntax GOSUB ON GOSUB {, } [ ELSE ] Associated keywords RETURN , ON Demonstration program First, here is a program to print out random phrases without using a subroutine: 100 REM A$ contains 7 words and each word 105 REM contains 5 characters - letters or spaces 110 A$="hand mouthear leg arm chestelbow" 120 FOR count=1 TO 10 125 REM pick a random number 130 R=RND(7)