Turbo Pascal 2.0 course cover
← Back to Courses
module
8

Names For Numbers

Introduction

Every sum in S7 was worked out and thrown away. A program that is going to do anything useful - count the moves in a game, keep a score - needs to hold on to a number, change it, and use it again. In Pascal that means a variable: a named place in memory to keep a value in.

Pascal is strict about variables, and it is a strictness that pays: every one must be announced before it is used, and Turbo refuses a program that forgets.

Declaring Variables

Variables are announced - declared - in a section of their own, between the program line and begin, headed var:

program Fruit;
var
  Apples, Pears: Integer;
  Total: Integer;
begin
  ...
end.

Each line gives one or more names, separated by commas, then a colon and the type - what kind of value the variable holds. Integer is the whole numbers of S7, from -32768 to 32767. There are other types, and they come in the sections ahead; the type is how Turbo knows what may go in the variable, and it checks every use against it.

Giving A Variable A Value

Inside begin and end, := puts a value into a variable:

Apples := 5;
Pears := 7;
Total := Apples + Pears;

Read := as "becomes": Apples becomes 5, Total becomes Apples plus Pears. The right-hand side is worked out first, with whatever values the variables have at that moment, and the answer goes into the variable on the left. So this is perfectly good Pascal:

Apples := Apples + 1;

"Apples becomes what Apples was, plus one." It is how a program counts.

It is :=, not =. Pascal keeps = for asking whether two things are equal, which is S12's business. Write Apples = 5; as a statement and Turbo stops:

Error 7: ':=' expected. Press <ESC>

A Variable Starts As Rubbish

A declared variable has no particular value until you give it one. Turbo does not set it to 0: it holds whatever happened to be in that piece of memory. Print one before you have set it and you may see 67, or 151, or anything else. Always give a variable a value before you use it.

Names

A name - Pascal calls it an identifier - is made of letters and digits, and starts with a letter. Apples, Total, Score2 are fine; 2Apples is not:

Error 58: Illegal character in identifier. Press <ESC>

Capitals do not matter. Total, TOTAL and total are the same variable to Turbo. This course writes names with a capital at the start of each word - Total, MaxMoves - because it reads well, but Turbo does not care, and a program that spells a name three ways will still work.

Two things Turbo will stop you doing. Using a name you did not declare:

Error 41: Unknown identifier or syntax error. Press <ESC>

which is the same message a misspelling gives, and usually is one. And declaring the same name twice:

Error 43: Duplicate identifier or label. Press <ESC>

And one thing Turbo will not let into an integer: a number with a fraction. Total := 7 / 2 is refused, because / always makes that other kind of number (S7):

Error 44: Type mismatch. Press <ESC>

Write And Writeln

Writeln prints and then ends the line. Write prints and stops, so the next thing printed follows on the same line:

Write('Apples and pears: ');
Writeln(Total);
Apples and pears: 12

And Writeln on its own, with no brackets at all, just ends the line - which prints an empty line if nothing was waiting.

The Code

program Fruit;
var
  Apples, Pears: Integer;
  Total: Integer;
begin
  Apples := 5;
  Pears := 7;
  Total := Apples + Pears;
  Write('Apples and pears: ');
  Writeln(Total);
  Apples := Apples + 1;
  Writeln('One more apple: ', Apples);
end.

Starting from

Turbo freshly started, Y, a new work file called FRUIT.

What you should see

Apples and pears: 12
One more apple: 6

Change One Thing

  • Delete the line Pears := 7;. Does the program still compile? What does it print for the total - and would it print the same on your machine?
  • Change Write('Apples and pears: '); to Writeln. Where does the 12 go?
  • Change Total := Apples + Pears; to Total := Apples / Pears;. What does Turbo say?

Exercises

8.1 Write a program with a variable Age, set to your age, that prints how old you will be next year.

8.2 Two variables A and B hold 1 and 2. Write a program that swaps them, so that A holds 2 and B holds 1, and prints both. (You will need a third variable.)

8.3 Write a program that sets a variable Count to 0, adds one to it three times, and prints it.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Error 41: Unknown identifier or syntax error on a variableIt is not declared under var, or it is spelt differently there.
Error 43: Duplicate identifier or labelThe same name declared twice.
Error 7: ':=' expectedYou wrote = where you meant :=.
Error 44: Type mismatchA number with a fraction (from /) going into an Integer. Use div.
Error 58: Illegal character in identifierA name starting with a digit.
A variable prints a strange numberIt was never given a value. Nothing sets it to 0 for you.

Summary

Variables are declared under var, with a type - so far, Integer. := gives a variable a value, worked out from the right-hand side first, so Count := Count + 1 counts. A variable holds rubbish until you set it. Names are letters and digits starting with a letter, and capitals do not matter. Write prints without ending the line.

Next

S9, Numbers With Fractions - the other kind of number, Real, and how to print it so that people can read it.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.