Pascal Programming/Variables and Constants
From Wikibooks, the open-content textbooks collection
[edit] Naming
... All names of things that are defined are identifiers. An identifier consists of 255 significant characters (letters, digits and the underscore character), from which the first must be an alphanumeric character, or an underscore ( _ ) ...
Van Canneyt, Michaƫl. Free Pascal: Reference guide.. Retrieved on 2008-08-17.
Pascal's identifiers are case-insensitive, meaning that for everything except characters and strings its case doesn't matter (THING, thing, ThInG, and etc are the same).
[edit] Constants
program Useless; const zilch = 0; begin (*some useless code...*) if zilch = zilch then (*...*) end.
Constants (unlike variables) can't be changed while the program is running. This assumption allows the compiler to simply replace zilch instead of wasting time to analyze it or wasting memory. As you may expect, constants must be definable at compile-time. However, Pascal was also designed so that it could be compiled in 1 pass from top to bottom(the reason being to make compiling fast and simple). Simply put, constants must be definable from the code before it, otherwise the compiler gives an error. For example, this would not compile:
program Fail; const zilch = c-c; c = 1; begin end.
but this will:
program Succeed; const c = 1; zilch = c-c; begin end.
It should be noted that in standard Pascal, constants must be defined before anything else (most compilers wont enforce it though). Constants may also be typed but must be in this form: identifier: type = value;.
[edit] Variables
Variables must first be declared and typed under the var like so:
var number: integer;
You can also define multiple variables with the same type like this:
var number,othernumber: integer;
Declaration tells the compiler to pick a place in memory for the variable. The type tells compiler how the variable is formatted. A variable's value can be changed at run time like this: variable := value. There are more types but, we will concentrate on these for now:
| type | definition |
|---|---|
| integer | a whole number from -32768 to 32767 |
| real | a floating point number from 1E-38 to 1E+38 |
| boolean | either true or false |
| char | a character in a character set (almost always ASCII) |