Skip to content

Remember a name; keep the program

Give a name a place in memory, use it in a greeting, and keep your work.

  • Sinclair ZX Spectrum
  • BASIC

Our greeting currently contains fixed words. We’ll give it a name it can use, then change that name without rewriting the instruction that prints it.

Start with the greeting

If you still have the three-line greeting from the opening lesson, keep it: we’ll replace lines 10 and 15 and leave line 20 in place. The Spectrum in this page starts empty: press Run it here at the end of the lesson, then Start, and enter the three lines below.

If you have been trying other programs, clear them before entering this lesson’s listing. Keep a saved copy of anything you want to retain first. At an empty input line, press A to enter NEW, then press Enter. This clears the stored program and its variables; clearing the screen alone would leave them in memory. You do not need to restart the Spectrum.

Starting from that empty program, enter all three lines below. Numbers store lines, P enters PRINT, and R at an empty input line enters RUN. The opening lesson explains keyword entry and editing in detail.

Give the name a place

Here is the complete greeting with its name stored in a variable:

step-01.bas
  10 LET n$="Sam"
  15 PRINT "Hello, ";n$
  20 PRINT "from the Spectrum"

At line 10, press L for LET, then type n. The symbols come from SYMBOL SHIFT, which is Alt (Option on a Mac): Alt+4 gives $, Alt+L gives = and Alt+P gives each quotation mark. In line 15, Alt+N gives the comma and Alt+O the semicolon. The Meet BASIC overview lists every symbol these lessons use.

LET n$="Sam" stores the text Sam in a variable named n$. A variable gives us a name by which to retrieve a stored value. LET assigns the value on the right of = to the variable on its left.

A string is text: a sequence of characters. Sinclair BASIC names a string variable with one letter followed by $. Here we chose n for “name”. The $ is part of the variable’s name; it does not mean money.

The quotation marks distinguish the literal text "Sam" from the variable name n$. Without quotes, BASIC looks for a value under a name. With quotes, it uses the characters themselves.

In line 15, PRINT displays the fixed text Hello, , then the value of n$. The semicolon makes the next item follow immediately. The space after the comma, inside the quotes, leaves a gap before the name.

Run the program:

The Spectrum displays Hello, Sam followed by from the Spectrum.
Line 10 supplies the name. Line 15 uses its value.

Foundations: A Place to Remember explores variables with pseudocode. This is Sinclair BASIC’s syntax for the same idea. Showing Your Work separates fixed text from a value being displayed. Its pseudocode punctuation is not a Sinclair BASIC rule: we use a semicolon here to keep the items together.

Change the value

Replace line 10 with 10 LET n$="Jo". Leave the other lines alone:

step-02.bas
  10 LET n$="Jo"
  15 PRINT "Hello, ";n$
  20 PRINT "from the Spectrum"
Line 15 has not changed. What will it print now?

Hello, Jo. When we run the program, line 10 assigns Jo to n$ before line 15 reads it. A variable is not a permanent label for its first value.

Run it to check. Then enter this separate instruction without a line number:

Sinclair BASIC
PRINT m$

The report 2 Variable not found means BASIC could not find that variable. We assigned n$, but asked for m$. The report names the immediate problem; checking our spelling explains this example. The stored program is intact: run it again to see the greeting.

Keep a copy outside the Spectrum

The program currently lives in the Spectrum’s working memory. Resetting the machine or closing an unsaved session can lose it. Keeping a copy lets us come back later and makes experiments less costly.

There are three different things we might keep:

  • A source listing, such as the .bas text shown here, records the numbered instructions in a file we can read in an editor on the host.
  • A tape image, such as a .tap file, holds data a Spectrum can load through its tape interface. BASIC’s SAVE stores the program and its variables.
  • An emulator snapshot preserves a machine state so the emulator can resume it. It is useful, but it is different from saving a program to tape.

On a Spectrum, the command is:

Sinclair BASIC
SAVE "greeting"

Press S for SAVE, enter the quoted name, then Enter. The Spectrum asks Start tape, then press any key. On original hardware this is the point to start recording on the cassette recorder before pressing a key.

The Spectrum asks Start tape, then press any key.
BASIC is waiting to send the program through its tape output.

Press Enter. The border flashes with stripes while the program goes out through the tape output, and 0 OK appears when it has finished. The Spectrum in this page has no recorder attached, so nothing keeps that recording. To keep a tape from the page, use the editor below. It holds this lesson’s listing as text: change it to match your program if yours differs, open Listing options, then press Download tape. Also copy the complete listing into a plain text file and save it so you can reopen that source in the browser. Your browser saves a file called greeting.tap.

Original code

Opens in your Spectrum.

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

Listing options

Original code

  10 LET n$="Jo"
  15 PRINT "Hello, ";n$
  20 PRINT "from the Spectrum"

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.

greeting is the name stored on the tape, the name SAVE and LOAD use. greeting.tap is the host file containing the tape data.

If you are working locally, Emu198x keeps the recording for you. Press Enter at the tape prompt and wait for 0 OK. Choose Tape → Export Recording… (or press Ctrl+Shift+E on Linux), select a folder you can find again and name the file greeting.tap. The confirmation tells you where it was written. If that filename already exists, choose a new one: export does not overwrite existing files. Export keeps every recording made in the current tape session; it does not empty the recorder.

Reopen your saved greeting

In the browser, reopen the plain text file you saved. Reload this lesson, replace the whole editable listing above with that saved source and choose Run this code. Check that your chosen name reappears. This tests the kept listing rather than the original example. The editable panel accepts source text; use an emulator with tape-loading controls to reopen the .tap file.

Working locally, close Emu198x and start it again. Choose File → Open Tape Deck… and select greeting.tap. This puts the tape in the deck; BASIC still needs to read it. On Linux, launch ./emu198x-spectrum --tape "path/to/greeting.tap" instead, using your exported file’s path and the same --rom option if needed. At an empty input line, press J for LOAD, then Alt+P twice for two quotation marks with nothing between them:

Sinclair BASIC
LOAD ""

Press Enter, then choose Tape → Play (F9 on Linux). Wait for loading to finish.

Now we can check what came back. Seeing a successful save report alone would not prove that we could recover our work.

Change the recovered program

After reopening the saved greeting, use K, Enter to list it. Check for Jo in line 10, then run it. Replace line 10 with 10 LET n$="Alex" and run again:

step-03.bas
  10 LET n$="Alex"
  15 PRINT "Hello, ";n$
  20 PRINT "from the Spectrum"

The result should begin Hello, Alex. The recovered copy is a program we can continue developing. Keep the changed version too when you want to resume from this point.

Next we’ll let the person running the program supply a name, using INPUT.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 2, “Basic programming concepts” (PRINT punctuation and the missing-variable example); chapter 7, “Expressions” (variable names); and chapter 20, “Tape storage”, pp. 141–145 (SAVE, LOAD, names and the recording prompt).

Run it here

This unit's program, built for you.

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

Download step-03.tap