Pascal Programming/Variables and Constants
Naming
Identifiers denote programmer defined names for specific constants, types, variables, procedures and functions, units, and programs. All programmer defined names in the source code –excluding reserved words– are designated as identifiers.
Identifiers consist of between 1 and 127 significant characters (letters, digits and the underscore character), of which the first must be a letter (a-z or A-Z), or an underscore (_).
(Michaël Van Canneyt (September 2017). "§1.4". Free Pascal Reference guide. version 3.0.4. p. 15. ftp://ftp.freepascal.org/pub/fpc/docs-pdf/ref.pdf. Retrieved 2019-12-14.)
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).
Constants
program Useless;
const
zilch = 0;
begin
(*some code*)
if zilch = zilch then
(*more code*)
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;
.
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) |