Declaring Variables

The basic syntax for a variable declaration is var identifierList : type;

where identifierList is a comma-delimited list of valid identifiers and type is any valid type. For example, var I: Integer;

declares a variable I of type Integer, while var X, Y: Real;

declares two variables - X and y - of type Real.

Consecutive variable declarations do not have to repeat the reserved word var:

X, Y, Z: Double; I, J, K: Integer; Digit: 0..9; Okay: Boolean;

Variables declared within a procedure or function are sometimes called local, while other variables are called global. Global variables can be initialized at the same time they are declared, using the syntax var identifier: type = constantExpression;

where constantExpression is any constant expression representing a value of type type. Thus the declaration var I: Integer = 7;

is equivalent to the declaration and statement var I: Integer; I := 7;

Multiple variable declarations (such as var X, y, Z: Real;) cannot include initializations, nor can declarations of variant and file-type variables.

If you don't explicitly initialize a global variable, the compiler initializes it to 0. Local variables, in contrast, cannot be initialized in their declarations and their contents are undefined until a value is assigned to them.

When you declare a variable, you are allocating memory which is freed automatically when the variable is no longer used. In particular, local variables exist only until the program exits from the function or procedure in which they are declared. For more information about variables and memory management, see Memory management.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment