Turbo Pascal 7.0 Guide
Variables - Storing Data
When programming, it is often necessary to store information so that it can be used at a later time in a program. In low level languages, the programmer specifies the exact location (in memory or in a particular register of the processor) where the information is stored. In high level languages, we use variables to represent stored information.
Imagine a series of boxes, each one with a label on the front to name the box. These names are called variables in Pascal. When a Pascal program starts, we need to specify both the name that we want to use to represent the stored information and the type of information that is going to be stored.
It is useful to give variables meaningful names that can be easily understood by the programmer (you).
There are rules about variable names.
Variable names must be a string of characters ( A - Z ) and can include digits ( 0-9 ) as well as the underscore character ( _ ). Variable names must begin with a letter or an underscore and cannot contain spaces or special characters.
Reserved Words
Some words should not be used as variable names. These are called reserved words and include words which have other meanings in Pascal.
The following words should not be used as variable names.
absolute | and | array | assembler |
asm | begin | case | const |
constructor | destructor | div | do |
downto | else | end | external |
far | file | for | forward |
function | goto | if | implementation |
in | inherited | inline | interface |
interrupt | label | mod | near |
nil | not | object | of |
or | packed | private | procedure |
program | public | record | repeat |
set | shl | shr | string |
then | to | type | unit |
until | uses | var | virtual |
while | with | xor |
Variable Types
We have to specify the type of information that our variables will be used to store. Pascal can store the following types of information (and a few more to meet later).
Type Of Information | Data Type Name | Example Data |
---|---|---|
numbers | integer | whole numbers eg 4, -9 |
numbers | real | numbers with a decimal point eg 3.14159, -1.5 |
text | char | a single character eg A, v, & |
text | string | a string of characters eg HELLO, !"£ |
logical | boolean | something that can be either true or false |
Declaring Variables
At the start of a program, the programmer declares the variables that are going to be used in the program. The format for a variable declaration is,
VAR variable list: type;
The following are examples of variable declarations.
VAR x, y, z: integer;
VAR A, b4: real;
VAR ZZ, CH: char;
VAR message: string[40];
All instructions in Pascal must end in a semicolon (;).
The string variable in the example declaration also specifies the maximum length that the string can be (in characters). If this information is not included, 256 characters are automatically allotted.