[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
typed constants          Initialized Variables                           [TP]

    Turbo Pascal allows you to declare variables and initialize them with
    specific values. These are known as "typed constants," probably
    because they appear in the CONST section of a program or subprogram.
    Even though they are called "constants," you can modify them as you
    would any other variable.

    Typed constants can save code if a constant is used in many places. By
    declaring the value as a typed constant, only one instance of the
    value will be generated, whereas an untyped constant (a normal
    constant) will appear every place the untyped constant is referenced.

    The general format is:

         const
           <vname>   : <type> = <value(s)>;

    where <vname> is the identifier, <type> is a regular type description
    (except that file and pointer types are not allowed), and <value(s)>
    is the list of initial values.

    If <type> is a simple type (Integer, Real, Boolean, Char, or
    enumerated), then a single value is given.

    If <type> is a string type, then <value> is a string constant
    (sequence of characters between single quotes).

    If <type> is an array type, then <values> is a list of values,
    separated by commas and enclosed by parentheses. For multi-dimensional
    arrays, each <value> is itself a list of values, separated by commas
    and enclosed by parentheses. The base type of an array can't be a file
    or pointer type.

    If <type> is a record type, then <values> is a list of field
    assignments, each of which takes the format "<field name> : <value>".
    They are separated by semicolons, and the entire list is enclosed by
    parentheses. The record can't contain fields that are file or pointer
    types. See the example below for an example of a record type.

    If <type> is a set type, then <values> is a set constant (list of
    values or value ranges, separated by commas and surrounded by square
    brackets).

  -------------------------------- Example ---------------------------------

           type
             MyRec = record
               Name : string[10];
               Cnt  : Integer;
               Flag : Boolean;
               Num  : Real
             end;

           const
             Iota : Real = 88.328;
             Able : array[1..5] of Char = ('a','e','i','o','u');
             Fred : MyRec = (Name : 'Fritz';
                             Cnt  : 22;
                             Flag : False;
                             Num  : 3.4);

See Also:
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson