[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 8.1.3 Constant Declaration

A constant declaration has the following look:

    const
      constant identifier = constant expression;
      ...
      constant identifier = constant expression;

A constant declaration part starts with the reserved word `const`. It declares a constant identifier which is defined by constant expression. This expression has to be evaluatable during compilation time, i.e. it can include numbers, parentheses, predefined operators, sets and type casts (the last, however, is a Borland extension). In ISO-7185 Pascal, constant expression must be a constant or a set. All Pascal Dialects but ISO-Pascal allow the use of these intrinsic functions in constant expression:

section 9.1 Abs, section 9.236 Round, section 9.282 Trunc, section 9.41 Chr, section 9.181 Ord, section 9.133 Length, section 9.201 Pred, section 9.271 Succ, section 9.257 SizeOf, section 9.176 Odd.

In Borland Pascal, in the constant declaration part variables can be declared as well, which are given an initial value. These variables are called "typed constants". It is good style to avoid this use, especially since Extended Pascal and GNU Pascal allow to initialize a variable in variable declaration part or give a type a preset value on declaration.

    const
      FiveFoo      = 5;
      StringFoo    = 'string constant';
      AlphabetSize = Ord ('Z') - Ord ('A') + 1;

    type
      PInteger     = ^Integer;     { Define a pointer to an Integer }

    const
      { Constant which holds a pointer to an Integer at address 1234 }
      AddressFoo   = PInteger (1234);

- BP does not know initialized variables, only typed constants. Even worse, it allows them to be misused as variables, without even warning. GPC supports this (unwillingly ;-), and warns unless in ``--borland-pascal'` mode.

  An example of a typed constant:

      const
        i: Integer = 0;

  If you want to use it as a constant only, that's perfectly fine. However, if you modify ``i'`, we suggest to translate the declaration to an initialized variable. The EP syntax is:

      var
        i: Integer value 0;

  GPC supports this as well as the following mixtureof dialects:

      var
        i: Integer = 0;

  Furthermore, you can also assign initialization values to types:

      program InitTypeDemo;

      type
        MyInteger = Integer value 42;

      var
        i: MyInteger;

      begin
        WriteLn (i)
      end.

  Here, all variables of type MyInteger are automatically initialized to 0 when created.

- Arrays initializers look like this in BP:

      program BPArrayInitDemo;

      const
        MyStringsCount = 5;

      type
        Ident = String [20];

      const
        MyStrings: array [1 .. MyStringsCount] of Ident =
          ('export', 'implementation', 'import',
           'interface', 'module');

      begin
      end.

  And the following way in EP:

      program EPArrayInitDemo;

      {$W no-field-name-problem} { avoid a warning by GPC }

      const
        MyStringsCount = 5;

      type
        Ident = String (20);

      var
        MyStrings: array [1 .. MyStringsCount] of Ident value
          [1: 'export'; 2: 'implementation'; 3: 'import';
           4: 'interface'; 5: 'module'];

      begin
      end.

  There seem to be pros and cons to each style. GPC supports both as well as just about any thinkable mixture of them.

  Some folks don't like having to specify an index since it requires renumbering if you want to add a new item to the middle. However, if you index by an enumerated type, you might be able to avoid major renumbering by hand.

### See also

section 8.1.6.4 Subroutine Parameter List Declaration

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Frank Heckenbach on May, 10 2002 using texi2html