Last Updated:

Variables and constants in Pascal

Variables and constants in Pascal

Pascal's original language was based on some simple concepts that have now become quite common in programming languages. The first is the concept of a data type. A type defines the values that a variable can have and the operations that can be performed on it.

The concept of type is stronger in Pascal than in C, where arithmetic data types are almost interchangeable, and much stronger than in the original versions of BASIC, which did not have a similar concept.


Pascal requires that all variables be declared before they are used. Each time you declare a variable, you must specify the data type. Here are some examples of variable declarations:

Value: Integer;
IsCorrect: Boolean;
C, D: Char;


The var keyword can be used in several places in your code, such as at the beginning of the code of a function or procedure, to declare variables, for local subroutines, or within a block to declare global variables. The word var is followed by a list of variable names followed by a colon and a data type name. You can write more than one variable name on a single line, as in the previous statement above.


Once you have defined a variable of this type, you can perform only operations on it that are supported by its data type. For example, you can use a Boolean value in a test and an integer value in a numeric expression. You can't mix Booleans and integers (as happens with C).

We can write the following code:

Valie:= 10;
isCorrect := True;

But the following statement is incorrect because the two variables have different data types:

Value:=isCorrect; error

If you try to compile this code, Delphi will throw a compiler error with the following description:

Incompatible types: 'Integer' and 'Boolean'.

Of course, you can often convert the value of a variable from one type to another. In some cases, this conversion happens automatically, but usually you need to call a certain system function that changes the internal representation of the data.

In Pascal, you can assign an initial value to a global variable when you declare it. For example, you can write:

Value: Integer = 10;
Correct: Boolean = True;

This initialization method works only for global variables, not for variables declared within the scope of a procedure or method.


Pascal also allows you to declare constants to name values that do not change at run time. To declare a constant, you do not need to specify a data type, but only assign an initial value. The compiler will look at the value and automatically use its correct data type. Here are some examples of declarations:

Thousand = 1000;
Pi = 3.14;
Author's name = 'Jinga;

Pascal determines the data type of a constant based on its value. The example above assumes that the Thousand constant is of type SmallInt, the smallest cumulative type that can contain it. If you want to use a specific type, you can simply add the type name to your ad, as in:

Thousand: Integer = 1000;

When you declare a constant, the compiler can choose whether to assign it a place in memory and store its value there, or duplicate the actual value each time the constant is used. This second approach makes sense, particularly for simple constants.


We have considered the basic concept of type in the Pascal language. But the language has another very important feature: it allows programmers to define new user-defined data types called user-defined data types.