GOSUB and Repetitious Messages

GOSUB to me still has something of the mystique that cursive handwriting has for young schoolkids.  It is emblematic of maturation and advancing skill.  Using GOSUB is a message to the world saying, “No longer does this wee programmer engage in spaghetti code.  I function on a higher plane!”  The truth is a little less exciting.  GOSUBs do take you away from the main logic of your program and it can take some effort to keep track of a program that branches out a lot with numerous subroutines.  Furthermore, if you choose to do so, you can go into just as deep a rabbit hole with GOSUB as you ever could with GOTO; subroutines never “have” to end (well, within GW-BASIC’s memory limits at least) and can themselves contain ever more subroutines and GOTOs.  However, you can also use GOSUB in such a way that each subroutine you write terminates and returns to the main program (or just ends the program).  Theoretically, you could do the exact same thing with GOTO, but it is easier to get in the habit of using RETURN statements to automatically return to execute the next line after a GOSUB statement since this does not require keeping track of line numbers.

One of my current projects that I’m working on is a multiple choice trivia game.  As a program, it does not really need to do a lot of branching out — I could write it without using any GOTO or GOSUB statements whatsoever.  It can proceed totally linearly, from one question to the next.  As a matter of fact, this is exactly what I have done in my program.  Each question displays a success or failure message; some are unique and some are repeated.  Going forward, though, I think using GOSUB to display repetitious success and failure messages would be the wiser choice if I ever decide to do a similar program in the future.  Let me give you an example of a world capitals game (not actually what I’m working on, but I might do one of those as well):

10 PRINT “What is the capital of Australia?”

15 PRINT “1. Duluth  2. Canberra  3. Sydney  4. Melbourne”

20 INPUT “Enter your choice:”; Q1%

25 IF Q1% = 2 THEN GOSUB 1000 ELSE GOSUB 2000

30 END

1000 PRINT “You are correct.”


2000 PRINT “That is incorrect.”


The practical value of using GOSUB here is you only need to enter the success and failure messages once into your program rather than entering them in after each question.  This can lead to significant space savings as you add more and more questions.  One of the downsides to using GW-BASIC is that you do need to keep your programs small to stay within the memory limit.  Using subroutines and functions intelligently can really help you keep your programs neat and trim.

This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to GOSUB and Repetitious Messages

  1. Pingback: Back to (GW-)basics – WriteAsync .NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s