Learn Pascal
1C - Constants


Constants are referenced by identifiers, and can be assigned one value at the beginning of the program. The value stored in a constant cannot be changed.

Constants are defined in the constant section:

     const
        Identifier1 = value;
        Identifier2 = value;
        Identifier3 = value;

Example:

     const
        Name = 'Tao Yue';
        FirstLetter = 'a';
        Year = 1997;
        pi = 3.1415926535897932;
        UsingNetscapeNavigator = TRUE;

The example has shown the main data types allowed in Pascal: strings, characters, integers, reals, and Booleans. Those data types will be further explained in the next section.

Note that in Pascal, characters are enclosed in single quotes, or apostrophes (')!

Constants are useful when you want to use a number in your programs that you know will change in the future. If you are writing a program for the British Parliament and want to display the name of the prime minister, you wouldn't want to hard-code the name every time you display it. It would be too time-consuming to find all the instances and change them later on. Instead, you would:
     const PrimeMinister = 'Winston Churchill';
and later change it to:
     const PrimeMinister = 'Tony Blair';
That way, the rest of your code can remain unchanged because they refer to the constant. Just the constant's value needs to be modified.

There's also a thing called typed constants. For example,

   const
      a : real = 12;
would yield an identifier a which contains a real value 12.0 instead of the integer value 12.

More about data types in the next section.


Previous lesson

Next lesson

Contents

Index

e-mail me


taoyue@mit.edu
Copyright © 1997-2001 Tao Yue. All rights reserved.