Skip to content

Write the repeated job once

Replace a duplicated heading with a routine that returns to its caller.

  • Sinclair ZX Spectrum
  • BASIC

The question screen and the answer screen belong to the same toy. We’ll give them the same heading, then avoid maintaining that heading in two places.

Continue with Give the answer a rhythm. If starting with an empty program, the first listing contains everything; the preceding two lessons explain its selection, dots and sound.

See the duplicated job

Add lines 51, 52, 89, 91 and 92, and replace line 90:

step-01.bas
   5 RANDOMIZE
  10 PAPER 0
  20 INK 7
  30 BORDER 0
  40 CLS
  50 PRINT "ORACLE STONE"
  51 PRINT "------------"
  52 PRINT
  60 INPUT "Ask a question: ";q$
  70 LET answer=INT (RND*3)+1
  80 CLS
  81 FOR i=1 TO 3
  82 PRINT ".";
  83 PAUSE 10
  84 NEXT i
  85 BEEP 0.05,0
  88 PRINT
  89 PRINT "ORACLE STONE"
  90 PRINT "------------"
  91 PRINT
  92 PRINT "THE STONE SAYS"
  95 INK 6
 100 IF answer=1 THEN PRINT "Try a small experiment."
 110 IF answer=2 THEN PRINT "Ask someone to join you."
 120 IF answer=3 THEN PRINT "Take a different route."
 130 INK 7

Lines 50–52 print the title, a divider and a blank line before the question. Lines 89–91 do exactly the same job after the dots. The answer label has moved to line 92. We have two identical blocks, reached at different moments.

Run it and compare the screens. To change the divider now, we would need to change both copies. Missing one edit would make the two screens disagree.

Give the job one home

Replace line 50 and line 89 with the calls below. Delete 51, 52, 90 and 91 by entering each number alone. Add 140 and 500–530:

step-02.bas
   5 RANDOMIZE
  10 PAPER 0
  20 INK 7
  30 BORDER 0
  40 CLS
  50 GO SUB 500
  60 INPUT "Ask a question: ";q$
  70 LET answer=INT (RND*3)+1
  80 CLS
  81 FOR i=1 TO 3
  82 PRINT ".";
  83 PAUSE 10
  84 NEXT i
  85 BEEP 0.05,0
  88 PRINT
  89 GO SUB 500
  92 PRINT "THE STONE SAYS"
  95 INK 6
 100 IF answer=1 THEN PRINT "Try a small experiment."
 110 IF answer=2 THEN PRINT "Ask someone to join you."
 120 IF answer=3 THEN PRINT "Take a different route."
 130 INK 7
 140 STOP
 500 PRINT "ORACLE STONE"
 510 PRINT "------------"
 520 PRINT
 530 RETURN

GO SUB 500 means “run the routine beginning at line 500, then come back”. A routine is a group of instructions doing a named job in our explanation; Sinclair BASIC identifies this one by its starting line number. Enter GO SUB with H after the line number. Enter RETURN with Y after its line number.

The first call at 50 runs 500, 510 and 520. RETURN at 530 resumes at line 60, where the question waits. The call at 89 runs the same three printing lines, but this time RETURN resumes at 92, before the answer label.

BASIC remembers the return point for each call. GO TO 500 would not do that: it changes the next line without arranging a return. The difference matters when the same job is used in more than one place.

Line 140 deliberately stops the main program. Without it, execution would continue into 500 after the answer, then reach RETURN without a matching call. Enter STOP with left Option/Alt+A after the line number.

Foundations: A Job With a Name uses named pseudocode routines. Our number 500 serves as the destination; it is a BASIC line number, not an address in the Spectrum’s memory.

What runs immediately after RETURN on the second call?

Line 92. BASIC returns to the instruction after the call at 89, not to the question at 60. The routine’s text is shared; the return point belongs to the particular call.

Change once and check twice

Replace the divider in line 510 with "============". Run and inspect both screens. Both should change because both call the same routine. Restore the hyphens if you prefer their lighter appearance.

The shared ORACLE STONE heading appears above the selected answer.
The shared ORACLE STONE heading appears above the selected answer.

The repeated job is small enough to understand completely. We have not added a general-purpose layout system: this routine prints one fixed heading wherever printing currently stands. It relies on the caller having selected readable colours and a suitable screen position.

Save with SAVE "oracle", keep its tape and load it into a fresh Spectrum, using the saving guide. In the page, make the editor below match your program and press Download tape for oracle.tap.

Original code

Opens in your Spectrum.

Edit this listing, then run it. Changes are marked against the original code.

Listing options

Original code

   5 RANDOMIZE
  10 PAPER 0
  20 INK 7
  30 BORDER 0
  40 CLS
  50 GO SUB 500
  60 INPUT "Ask a question: ";q$
  70 LET answer=INT (RND*3)+1
  80 CLS
  81 FOR i=1 TO 3
  82 PRINT ".";
  83 PAUSE 10
  84 NEXT i
  85 BEEP 0.05,0
  88 PRINT
  89 GO SUB 500
  92 PRINT "THE STONE SAYS"
  95 INK 6
 100 IF answer=1 THEN PRINT "Try a small experiment."
 110 IF answer=2 THEN PRINT "Ask someone to join you."
 120 IF answer=3 THEN PRINT "Take a different route."
 130 INK 7
 140 STOP
 500 PRINT "ORACLE STONE"
 510 PRINT "------------"
 520 PRINT
 530 RETURN

Your Spectrum

48K · PAL

Click the screen to send it keys. Tab returns to the controls.

Not started.

On-screen keys
Keyboard and Session

Shift is CAPS SHIFT. Alt or Option is SYMBOL SHIFT. Comma and full stop use their Spectrum key combinations automatically. Escape sends BREAK while the screen has focus. Enter submits a line. Restart clears this session. Returning to the lesson pauses it.

To type into this screen, use a physical keyboard. On a touchscreen, edit the listing in the lesson and choose Run this code. Open On-screen keys here to answer numeric INPUT prompts or send BREAK.

Runs the Sinclair 48K ROM. Amstrad have kindly given their permission for the redistribution of their copyrighted material but retain that copyright.

Download Emu198x for desktop
Screen text
The Spectrum has not started yet.

Check that both screens appear and the toy ends with 9 STOP. Use RUN when you want another consultation. It does not need an automatic replay loop to be finished.

Ask someone whether the wording makes clear that the response is chosen, not reasoned out. That honesty is part of the design. Our next project will let a person control a position rather than type an answer.

Next, Put it somewhere begins a new program.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 5, “Subroutines”, pp. 37–38; chapter 15 for PRINT.

Run it here

This unit's program, built for you.

Program
step-02.tap
Machine
ZX Spectrum 48K (PAL)

Download step-02.tap