ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 14.2 - Subroutines

Exercises

1. The example program is virtually a universal LSD calculator. How would you use it.

    (i) to convert pounds & new pence into pounds, shillings & pence?

    (ii) to convert guineas into pounds & shillings? (1 guinea = 1..1s)

    (iii) to find fractions of a pound? (e.g. a third of a pound, or a mark, is 6s..8d.

Put in a line to round the pence off to the nearest farthing (1/4d).

2. Add two statements to the program:

4 LET ADJUST=1000

7 LET LSDPRINT=2000

& change

GOSUB 1000 to GOSUB ADJUST

GOSUB 2000 to GOSUB LSDPRINT

This works exactly as you'd hope; in fact the line number in a GOSUB (or GOTO or RUN) statement can be any numerical expression. (Don't expect this to work on computers other than the ZX81, because it is not standard BASIC.)

This sort of stuff can work wonders for the clarity of your programs.

3. Rewrite the main program in the example to do something quite different, but still using the same subroutines.

4.    .. GOSUB n

      ... RETURN

in consecutive lines can be replaced by

      ... GOTO n

Why?

5. A subroutine can have several entry points. For instance, because of the way our main program uses them, with GOSUB 1000 followed immediately by GOSUB 2000, we can replace our two subroutines by one big one that adjusts L, S & D & then prints them. It has two entry points: one at the beginning for the whole subroutine, & another further on for the printing part only.

Make the necessary rearrangements.

6. Run the program:

10 GOSUB 20

20 GOSUB 10

The return addresses are pushed on to the GOSUB stack in droves, but they never get taken off again & eventually there is no room for any more in the computer. The program then stops with error 4 (see appendix B).

You might have difficulty in clearing them out again without losing everything, but this will work.

    (i) Delete the two GOSUB statements.

    (ii) Insert two new lines

      11 RETURN

      21 RETURN

    (iii) Type

      RETURN

The return addresses will be stripped off until you get error 7.

    (iv) Change your program so you don't get the same thing happening again.

How does this work?

Sinclair ZX Spectrum

  Previous Page Back Next Page