Classic Computer Magazine Archive COMPUTE! ISSUE 86 / JULY 1987 / PAGE 54

The Beginners Page

Tom R. Halfhill, Editor

Using Variables

I've recently had several letters asking about variables, so we'll examine that topic this month.

A variable name is a label given to a value that represents a number or string. The value of the variable may change while the program is running.

For example, in the early part of a program you may define N to be 5 (N = 5), so variable N will have the value of 5. Later in the program you may redefine N with N = 8. You may also use an expression such as N = N + 1. This expression means to replace value N with N + 1. If N was 8, it becomes 8 + 1, or 9.

You will want to use a variable name whenever you are working with numbers or strings that could change. For example, you could use the variable S for Score. As points are made, you increment S.

A string variable name ends with a dollar sign. For example, you may read in from DATA a value for NAME$, print NAME$, read in another NAME$, and so forth.

Variables And Constants

Many statements specify parameters which may be numeric constants or variables. Using variables allows changes to be made rather easily. For example, consider drawing graphics on the screen. Let's say you want to draw a train car that has four wheels visible. The center of the first circle can be specified with variables X and Y. The center of the next circle can be relative to the first, with X + 20 and Y. The third circle may be X + 80 and Y, and the fourth may be X + 100 and Y. Before you draw the four circles, you need to define X and Y, so you can use the CIRCLE command to draw the wheels. Now let's suppose the wheels are not positioned quite right. They need to be higher and a bit more toward the center of the screen. If you had used constants in the CIRCLE commands, you would need to change the constants in the four CIRCLE commands. However, by using variables, all you need to do is change the definitions of X and Y.

Another place I like to use a variable is as a timing function in music commands. Use a variable such as N for a quarter note. An eighth note would be N/2, and a whole note, N * 4. You can write all the sound commands in terms of that variable duration and define N at the beginning of the program. To speed up or slow down the whole piece, you simply need to change one statement defining the duration N—not every sound command.

Another example of variable usage is to define the variables and then call a subroutine that uses them. For example, you might have a subroutine that draws a hexagon with a starting point of row R and column C and with the length of a side equal to S. To draw a hexagon on the screen, simply define R, C, and S; then GOSUB for the drawing routine. To draw another hexagon, redefine R, C, and S; then GOSUB.

Naming Variables

How do we name variables? Most versions of BASIC specify that a variable name must contain alphanumeric characters (letters plus numbers) and must start with an alphabetic character. Some BASICs allow certain symbols such as the underline or the at sign (@) to be part of the variable name.

There are just a couple of restrictions. Some computers recognize only the first two characters of a variable name, even though the name may actually be longer. In such a case, the variables BLACK and BLUE would be considered the same variable—BL. Some computers do not allow a variable name to contain a reserved keyword. For example, FORK, KNIFE, and SPOON would not be acceptable variable names because they have the embedded keywords FOR, IF, and ON. Newer computers are allowing longer variable names and aren't so picky about embedded words.

Also, BASIC does not recognize a difference between lowercase and uppercase letters. Thus, STARSHIP, Starship, and starship all represent the same variable.

Many of the early BASIC programmers started programming in FORTRAN. Any integer variable in FORTRAN had to start with I, J, K, L, M, or N. Out of habit, the programmers carried over that tradition to BASIC and used those letters for variable names in FOR-NEXT loops or as counters or flags. You can choose much more meaningful variable names, such as FOR ROW = 1 TO 24 … NEXT ROW.

Another habit from the early days of micros was using A for the first variable used in a program, B for the second, and so on. Back when computers had small memories, it really helped to use variable names with just one letter. I also recall that some of the early machines tracked the variables in alphabetic order, so it was faster to use variables from the first part of the alphabet.

Now, with faster and larger-memory computers, the trends for naming variables are changing. Most programmers like to use variables that mean something. Rather than A, B, C, X, and Y, you might see names such as AREA, LENGTH, NUMBER, FLAG, SCORE, and HIGH__SCORE. This is a way of documenting your program. Also, you'll find it much easier to keep track of variables if you name them meaningfully—and using meaningful variable names is one of the advantages often listed for programming in Pascal. Now you can do the same in BASIC.