Variables are a fundamental programming concept that you have to master before you can start writing useful applications. Simply put, a variable is a name for a segment of memory that we can read data from and write data to. The primary job of a variable is to enable the programmer to work with a piece of data while the application is running.
Before we can use a variable in a Delphi application we have to declare it.
There are three steps that you need to complete in order to properly declare a variable:
Use the reserved word var to let Delphi know that you are declaring one or more variables.
Specify a name for the variable.
Specify a data type for the variable.
The basic syntax for a variable declaration is:
var VariableName: DataType;
Here is an example declaration of a variable that can hold a string value:
var s: string;
If you want to declare more than one variable, you don't have to place the reserved word var in front of every variable declaration. Here is the preferred way of declaring several variables at once:
var s: string; I: Integer; c: Char;
If you need to declare more than one variable of the same type, you can declare them all in one line:
var str1, str2, str3: string; I, I2: Integer;
The most important thing to note about variables in Delphi is that they have to be declared before they are used. Delphi doesn't allow implicit variable declarations like Visual Basic or declarations inside of a block like C++. The variable declaration block must be written before the block in which we wish to use our variables. In a console application, the var block must precede the main block.
Listing 2-8: Variable declarations
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var str1, str2, str3: string; I, I2: Integer; begin ReadLn; end.
Delphi has two flavors of variables: global and local. All variables in the var block in Listing 2-8 are global variables. There are some differences between the two types of variables, but these are described in more detail in Chapter 5 in the "Local and Global Variables" section.
An identifier is a word that is used to name certain elements of an application — variables, constants, units, and others. For a word to qualify as an identifier it must follow these simple rules:
Can be of any length, but only the first 255 characters are significant
Can contain letters, numbers, and underscores
Must begin with an underscore or a letter but never a number
Cannot contain spaces
Normally, an identifier cannot be the same as one of the reserved words; to use a reserved word as an identifier, you need to prefix the identifier with &:
var &begin: string;
Delphi identifiers are not case sensitive like identifiers in C++, so you cannot declare two variables with identical names but different capitalization:
{ The following two declarations result in a compiler error - Identifier redeclared. } var str: string; STR: string;
The assignment operator in Delphi is the symbol ":=" (a colon followed by the equal sign). The assignment operator is used in an assignment statement to write a new value to a variable. The syntax of the assignment statement is:
Variable := Value;
or
Variable := Expression;
The following example shows how to write simple assignment statements. Notice how easy it is to display the contents of a variable using the WriteLn statement.
Listing 2-9: Simple assignment statements
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var x: Integer; xSquared: Integer; begin x := 3; xSquared := x * x; WriteLn(xSquared); ReadLn; end.
When you run the above code, the application will display 9, which is the correct result of this simple assignment.