Basic Pascal Tutorial/Chapter 1/Constants

From Lazarus wiki
Revision as of 05:14, 23 December 2019 by Rfc1394 (talk | contribs) (ADD ARRAYS)
Jump to navigationJump to search

български (bg) Deutsch (de) English (en) français (fr) italiano (it) 日本語 (ja) 한국어 (ko) русский (ru) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

1C - Constants (author: Tao Yue, state: changed)

(update 2019-12-22 Paul Robinson to add array 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 come in two flavors: Scalars and arrays. A scalar is a single variable which is assigned a single value. Array constants hold multiple values. They will be explained in greater detail in separate sections below.

Scalar constants

Scalar constants are defined in the Const (constant) section of the program:

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

For example, let's define some constants of various data types: strings, characters, integers, reals, and Booleans. These data types will be further explained in the next section.

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

Note that in Pascal, characters are enclosed in single quotes, or apostrophes (')! This contrasts with newer languages which often use or allow double quotes or Heredoc notation. Standard Pascal does not use or allow double quotes to mark characters or strings.

Constants are useful for defining a value which is used throughout your program but may change in the future. Instead of changing every instance of the value, you can change just the constant definition.

Typed constants force a constant to be of a particular data type. For example,

const
  a : real = 12;

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


Array constants

Array constants work much the same as scalar constants, except multiple values can be specified. All of the values must be the same type, whether it is a number (byte, word, integer. real, etc.) or character based (char, string, ansistring, etc.)

One-dimensional arrays

For one-dimensional array constanta. the general format is:

const
identifier: array[low_value .. high_value] of type = ( values );
identifier

Where

  • identifier is the name of the array;
  • low_value is the lower bound of the array;
  • high_value is the upper bound of the array;
  • type is the type of value stored in the elements of the array (char, integer, real, string, etc.) and
  • values is a list of values with each item in the list separated from the next itwm by a comma.

Here's a simple one, the alphabet:

const
  Alphabet: array [1..26] of char =
       ('A','B','C','D','E','F','G','H','I',
        'J','K','L','M','N','O','P','Q','R',
        'S','T','U','V','W','X','Y','Z'   ) ;

There are two rules to remember. As was said before, all the values given have to be the same, and you have to specify as many values as there are elements in the array. While the example given above is okay, there will be other parts of the program that reference this array, such as for loops. etc. Instead of using the value '26' for the size of the array, let's use a scalar constant instead:

Const
   LetterCount = 26;
   Alphabet: array [1..LetterCount] of char =
       ('A','B','C','D','E','F','G','H','I',
        'J','K','L','M','N','O','P','Q','R',
        'S','T','U','V','W','X','Y','Z'   ) ;

Now, anyplace you need to reference the size of the array, you can just use LetterCount.

Here's a more interesting example, in which several datatypes are used. This example, which is probably from a calendar program, has all sorts of types: char, string, and integer.

Const
  MonthStart = 0 ; // could be 1 and
  MonthEnd   = 11; // 12 if desired
  DayStart   = 0 ; // same,could be 1 and
  DayEnd     = 6;  // 7
  DayNameCh: array [DayStart .. DayEnd] of char =(
        'S','M','T','W','H','F','A');
  DayNameShort: array [DayStart .. DayEnd] of string=
    ( 'Sun','Mon','Tue','Wed','Thu',
      'Fri','Sat' ) ;
  DayNameLong: array DayStart .. DayEnd] of 
    string = ( 'Sunday', 'Monday','Tuesday','Wednesday',
               'Thursday', 'Friday', 'Saturday' );
     MonthNameLong: array[MonthStart ..MonthEnd] of string = (
  'January','February','March','April',
  'May','June','July','August',
  'September','October','November'.
  'December'  
             );
   MomthDays: ARRAY [ MonthStart .. MonthEnd ] of
    integer = ( 31, 28, 31, 30, 31, 30,
                31, 31, 30. 31, 30, 31 );

Notice that for the items indicated as type string, while every item had to be a string, they were not required to all be the same size.


 ◄   ▲   ►