Variables and the five types
Naming values in a var block, the five types a beginner needs, and how each one is printed — including the decimal that needs a format.
Nothing exists until you declare it
Pascal has no implicit variables. Every name has to be declared in a var block before the begin that uses it, with the type written after a colon: age: integer;. Using an undeclared name is a compile error, not a silent zero.
One var keyword opens the block and each line inside names one or more variables of a single type. The block ends where the next declaration keyword or the begin starts, so there is no closing bracket to forget.
Names are not case sensitive. age, Age and AGE are the same variable, and the same is true of the keywords themselves, though the convention is lower case for both.
The types worth knowing first
A type fixes what a value may be and what you may do with it. These five cover almost everything a beginner writes, and the compiler will not let you quietly store one in another.
The list is short on purpose. A beginner can write a term of work with these five, and each one maps to something visible in the output: a count, a price, a label, a yes-or-no answer, a single letter. The numeric widths and the structured types can wait until a program actually needs them.
integer— a whole number, positive or negative, with no fractional part.real— a decimal number, written with a point, like2.5.string— text, written between single quotes.boolean—trueorfalse, and nothing else.char— exactly one character, also between single quotes.
Printing each kind
Whole numbers, text, booleans and characters print as they are. A boolean is the one worth knowing: Free Pascal writes it in capitals, as TRUE and FALSE, even though you typed lower case.
A real is different. Printed plainly it comes out in a long scientific form with more digits than you will ever want, so a real is given a width and a count of decimals: price:0:2 means no padding and two decimals.
The same colon works for the other types when you want columns: name:8 pads the text on the left to eight characters, which is how a report lines up without counting spaces by hand.
The width is a minimum, not a limit. name:8 pads a short name and prints a long one in full, so one wide value can push a column out of line. That is usually better than cutting text off, and it is why a report format is chosen from the longest value rather than the shortest.
One of each
program Main;
var
age: integer;
price: real;
begin
age := 36;
price := 2.5;
writeln('Age ', age);
writeln('Price ', price:0:2);
end.Why the type is checked first
A declaration does two jobs at once: it reserves storage and it fixes the operations the compiler will allow. An integer can be added to another integer but cannot be sliced with copy; a string can be joined with + but cannot be divided. A mismatch is reported before the program runs, not discovered halfway through.
The types also differ in what they can hold. An integer has a finite range, and stepping past it wraps or raises depending on the build. A real stores an approximation in binary, so 0.1 is not exact and two calculations that should agree may differ in the last digit. That is why money is often kept in whole units and given a decimal point only when it is printed.
A name exists before its first value
Declaring a variable reserves its storage when the program starts, but it does not put a value you chose there. A global integer happens to begin at zero in Free Pascal, while a local variable can hold whatever the previous call left on the stack. Neither is an initial value the program stated, and the difference is exactly the kind of thing that changes when code moves into a routine.
That is why every example assigns a variable before it prints one. The habit is cheap, and it removes a whole class of bug in which a total starts at a strange number because one branch forgot to set it. If a value has a sensible starting point, write the assignment beside the declaration and make it obvious to the next reader.
Three type slips worth recognising: assigning 2.5 to an integer (rejected, not quietly truncated), using = where := belongs (the compiler reads a comparison and then an unexpected statement), and printing a real without :0:2, which gives a long scientific form rather than the price you expected.
Try these next
- Declare a second integer and print their sum and their
divresult. - Change
pricefrom2.5to2.0, then to2, and note that Pascal accepts an integer where a real is expected. - Print a
charand a one-characterstring, and compare how they look.
One variable of each type
Run it, then change the age, the price and the grade. Watch how the real is printed with a format and what the boolean and char print on their own.
Change price to 19.5 and the format to :0:1, then to no format at all. The bare form is the one that shows why decimals are always formatted.
A till slip with a real and a boolean
Run it, then change the quantity to 4 and the unit price to 3.75. Predict both formatted lines before you run it again.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
Where do the variables of a program go?
