Chapter 6 of 12
Give a routine a job
Think of the last thing a pilot ought to do after landing. The next crew needs a record of the trip. What if the flight program could simply ask someone to file that record and come back when the job was done?
Leave a place to come back to
Open VOYAGE.bas. First read this little piece of the main flight plan.
135 GOSUB 300
140 PLAY "T180O3L8CEG"
150 GOSUB 200
160 PRINT "LOG SAVED FOR ";P$(D)
170 END
A subroutine is a group of instructions with a job of its own. GOSUB 300 sends BASIC to the landing routine. BASIC remembers where to come back: when the landing routine reaches RETURN, the next instruction after the call is the PLAY at line 140. The next call, GOSUB 200, sends BASIC to the filing routine. Its RETURN brings us back to line 160.
Turn the two calls into words and you can read the whole flight plan: land; play a tune; file the log; tell the pilot it was saved. The filing routine happens to be earlier than the landing routine in the listing. You can read them in the order that the pilot uses them. Line 170 ends the main plan, so BASIC leaves the code below it alone until a GOSUB asks for a job.
200 OPEN "O",#1,"VOYAGE.DAT:0"
210 WRITE #1,P$(D),S
220 CLOSE #1
230 RETURN
The file number #1 connects the open, write and close instructions. Both the main program and the routine know the destination number D, the name array P$, and the supplies S. A GOSUB in this BASIC shares its caller’s variables. That makes a short routine easy to call. It also means changing S inside the routine would change what the crew sees afterward.
Step into the file room
Use Disk BASIC with a disposable initialized floppy mounted in FD0. In Media → Floppy Disks…, check the name of that disk and look for Saves automatically. VOYAGE writes a file called VOYAGE.DAT and replaces an earlier flight under that name. If you don’t have a practice disk yet, make one before running this step.
- In the VOYAGE editor, mark line 150 as the only breakpoint. Add D and S to Watches. Choose Run.
- Choose 2 and press Return at the helm. When the Mars chart asks you to land, press A on the CoCo keyboard. Look at the editor when it stops on line 150. D is 2, and S is 3.
- Guess which line Step Into BASIC statement will highlight. Try it. The highlight moves to 200, the first instruction of the file job. The crew hasn’t announced a save yet.
- Choose Step Out of BASIC subroutine. The CoCo finishes the job, including CLOSE. It stops on line 160, where the main flight plan resumes. Choose Continue and look for LOG SAVED FOR MARS.
Choose how much of a job to watch
Leave the breakpoint at 150. Fly again, this time entering 1 for Earth and pressing A to land. At the stop, choose Step Over BASIC subroutine. Predict the next highlighted line. You land on 160 again, this time without a pause at 200. The filing job still ran. Choose Continue and you see LOG SAVED FOR EARTH. Remove the breakpoint when you finish this exercise.
Use Step Into to see how a job works, Step Over when you already trust the job, and Step Out when you’ve seen enough of the current routine. A breakpoint you deliberately set inside a routine can still bring Step Over to a stop there. A GOTO, unlike GOSUB, makes no promise to come back.
As your programs grow, ON … GOSUB can use a number to select one of several routines. DEF FN gives a name to a calculation that returns a value; BASIC’s own LEN and INT are familiar examples of how it feels to use a function. Look up these forms in the language reference. The flight program already has two useful jobs, and you can follow both of them all the way back to the cockpit.