Ad Space — Top Banner

2 Variable not found

Sinclair ZX Spectrum

Severity: Minor

What Does This Error Mean?

Error 2 on the ZX Spectrum means the program tried to use a variable that has not been assigned a value yet. Make sure every variable is assigned before it is first used — Spectrum BASIC does not default variables to zero like some other BASICs.

Affected Models

  • Sinclair ZX Spectrum 48K
  • Sinclair ZX Spectrum 128K
  • Spectrum+
  • Spectrum +2
  • Spectrum +3
  • ZX Spectrum Next
  • Fuse emulator

Common Causes

  • A variable was used in an expression before being given a value with LET or INPUT
  • A FOR loop variable referenced in the program body before the FOR line has been reached
  • A variable name typo — using A where the assigned variable was AA
  • The program was edited and the assignment line was accidentally deleted

How to Fix It

  1. Find the line that caused the error using LIST.

    The Spectrum highlights the error line. Look for the variable name that is used and search earlier in the program for where it should have been assigned.

  2. Add a LET statement before the first use of the variable.

    For example, if the program uses LET SCORE = SCORE + 10 but SCORE was never initialised, add: 10 LET SCORE = 0 at the start of the program.

  3. Check for typos — variable names on the Spectrum are case-sensitive in display but not in execution.

    Spectrum BASIC allows multi-letter variable names. LET score = 0 and LET SCORE = 0 are the same variable, but LET scor = 0 is different from LET score = 0.

  4. Ensure FOR loop variables are not used before the FOR statement is reached.

    If you reference the loop variable I before line 100 FOR I = 1 TO 10, error 2 occurs. Move the reference to after the FOR line, or initialise the variable before the loop.

Frequently Asked Questions

Does ZX Spectrum BASIC auto-initialise numeric variables to zero?

No — unlike Commodore BASIC, Sinclair BASIC does not automatically set unassigned variables to zero. Using a variable before assigning it always causes error 2.

Can I have multi-letter variable names on the Spectrum?

Yes. Spectrum BASIC supports variable names of any length for numeric variables (e.g. SCORE, LIVES, PLAYERHEALTH) and two-character names for string variables (e.g. A$, N$). String variables are limited to one letter plus the $ sign.