RISCOS.com

www.riscos.com Technical Support:
BBC BASIC Reference Manual

 


Variables and expressions


A variable has a name and a value associated with it. The name, for example, FRED or a single letter such as x, allows the variable to be identified and its value to be accessed. This value can be changed and retrieved as many times as required.

Types of variables

There are three different types of variables used to store different types of information. These are:

  • Integer variables which can only store whole numbers
  • Floating point variables, which can store either whole numbers or fractions
  • String variables which store characters.

Each type is distinguished by the last character of the variable name. A name by itself, like Fred, signifies a floating point variable; Fred% is an integer variable, and Fred$ is a string variable.

Naming variables

The rules for naming variables are as follows:

  • there must be no spaces within the name
  • they can contain digits and unaccented upper- and lower-case letters
  • they can be divided into multiple words, using the underscore character (_)
  • they must not start with a digit
  • they must not start with any BASIC keywords.

All the following names are allowed:

X
xpos
XPOS
Xpos
x_position
greatest_x_position
position_of_X
XPOS1

Note that upper- and lower-case letters are regarded by BASIC as being different, so that XPOS, xpos and Xpos are three separate variables.

The following names are not allowed:

2pos It does not begin with a letter.
TOTAL_x It begins with TO, a BASIC keyword.
FOREST It begins with FOR, a BASIC keyword.
COST It begins with COS, a BASIC keyword.
x-pos It contains a minus sign.
X Position It contains a space.
X.pos It contains a punctuation mark.

It is very easy to be caught out by the rule which says that the variables must not start with a BASIC keyword. The best way to avoid this problem is to use lower- or mixed-case variable names since BASIC keywords only use upper-case. This has the added advantage of making the program easier to read.

The values of the current variables may be displayed at any time by typing the command LVAR at the BASIC prompt and then pressing Return.

This edition Copyright © 3QD Developments Ltd 2015
Last Edit: Tue,03 Nov 2015