﻿ Declared Constants
Declared Constants

Several different language constructions are referred to as 'constants'. There are numeric constants (also called numerals) like 17, and string constants (also called character strings or string literals) like 'Hello world!'. Every enumerated type defines constants that represent the values of that type. There are predefined constants like True, False, and nil. Finally, there are constants that, like variables, are created individually by declaration.

Declared constants are either true constants or typed constants. These two kinds of constant are superficially similar, but they are governed by different rules and used for different purposes.

A true constant is a declared identifier whose value cannot change. For example,

`const MaxValue = 237;`

declares a constant called MaxValue that returns the integer 237. The syntax for declaring a true constant is

const identifier = constantExpression

where identifier is any valid identifier and constantExpression is an expression that the compiler can evaluate without executing your program.

If constantExpression returns an ordinal value, you can specify the type of the declared constant using a value typecast. For example

`const MyNumber = Int64(17);`

declares a constant called MyNumber, of type Int64, that returns the integer 17. Otherwise, the type of the declared constant is the type of the constantExpression.

• If constantExpression is a character string, the declared constant is compatible with any string type. If the character string is of length 1, it is also compatible with any character type.
• If constantExpression is a real, its type is Extended. If it is an integer, its type is given by the table below.

Types for integer constants

 Range of constant (hexadecimal) Range of constant (decimal) Type Aliases 0..\$FF 0..255 Byte UInt8 0..\$FFFF 0..65535 Word UInt16 0..\$FFFFFFFF 0..4294967295 Cardinal UInt32, LongWord 0..\$FFFFFFFFFFFFFFFF 0..18446744073709551615 UInt64 -\$80..\$7F -128..127 ShortInt Int8 -\$8000..\$7FFF -32768..32767 SmallInt Int16 -\$80000000..\$7FFFFFFF -2147483648..2147483647 Integer Int32, LongInt -\$8000000000000000..\$7FFFFFFFFFFFFFFF -9223372036854775808..9223372036854775807 Int64

32-bit native integer type

 Range of constant (hexadecimal) Range of constant (decimal) Type Equivalent type -\$80000000..\$7FFFFFFF -2147483648..2147483647 NativeInt Integer 0..\$FFFFFFFF 0..4294967295 NativeUInt Cardinal

64-bit native integer type

 Range of constant (hexadecimal) Range of constant (decimal) Type Equivalent type -\$8000000000000000..\$7FFFFFFFFFFFFFFF -9223372036854775808..9223372036854775807 NativeInt Int64 0..\$FFFFFFFFFFFFFFFF 0..18446744073709551615 NativeUInt UInt64

Here are some examples of constant declarations:

```const
Min = 0;
Max = 100;
Center = (Max - Min) div 2;
Beta = Chr(225);
NumChars = Ord('Z') - Ord('A') + 1;
Message = 'Out of memory';
ErrStr = ' Error: ' + Message + '. ';
ErrPos = 80 - Length(ErrStr) div 2;
Ln10 = 2.302585092994045684;
Ln10R = 1 / Ln10;
Numeric = ['0'..'9'];
Alpha = ['A'..'Z', 'a'..'z'];
AlphaNum = Alpha + Numeric;```

Constant Expressions

A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. Constant expressions include numerals; character strings; true constants; values of enumerated types; the special constants True, False, and nil; and expressions built exclusively from these elements with operators, typecasts, and set constructors. Constant expressions cannot include variables, pointers, or function calls, except calls to the following predefined functions:

 Abs High Low Pred Succ Chr Length Odd Round Swap Hi Lo Ord SizeOf Trunc

This definition of a constant expression is used in several places in Delphi's syntax specification. Constant expressions are required for initializing global variables, defining subrange types, assigning ordinalities to values in enumerated types, specifying default parameter values, writing case statements, and declaring both true and typed constants.

Examples of constant expressions:

```100
'A'
256 - 1
(2.5 + 1) / (2.5 - 1)
'CodeGear' + ' ' + 'Developer'
Chr(32)
Ord('Z') - Ord('A') + 1```

Resource Strings

Resource strings are stored as resources and linked into the executable or library so that they can be modified without recompiling the program.

Resource strings are declared like other true constants, except that the word const is replaced by resourcestring. The expression to the right of the = symbol must be a constant expression and must return a string value. For example,

```resourcestring
CreateError = 'Cannot create file %s';
OpenError = 'Cannot open file %s';
LineTooLong = 'Line too long';
ProductName = 'CodeGear Rocks';
SomeResourceString = SomeTrueConstant;```

Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types. Typed constants cannot occur in constant expressions.

Declare a typed constant like this:

const identifier: type = value

where identifier is any valid identifier, type is any type except files and variants, and value is an expression of type. For example,

`const Max: Integer = 100;`

In most cases, value must be a constant expression; but if type is an array, record, procedural, or pointer type, special rules apply.

Array Constants

To declare an array constant, enclose the values of the array's elements, separated by commas, in parentheses at the end of the declaration. These values must be represented by constant expressions. For example,

`const Digits: array[0..9] of Char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); `

declares a typed constant called Digits that holds an array of characters.

Zero-based character arrays often represent null-terminated strings, and for this reason string constants can be used to initialize character arrays. So the previous declaration can be more conveniently represented as

`const Digits: array[0..9] of Char = '0123456789';`

To define a multidimensional array constant, enclose the values of each dimension in a separate set of parentheses, separated by commas. For example,

```type TCube = array[0..1, 0..1, 0..1] of Integer;
const Maze: TCube = (((0, 1), (2, 3)), ((4, 5), (6,7)));```

creates an array called Maze where

```Maze[0,0,0] = 0
Maze[0,0,1] = 1
Maze[0,1,0] = 2
Maze[0,1,1] = 3
Maze[1,0,0] = 4
Maze[1,0,1] = 5
Maze[1,1,0] = 6
Maze[1,1,1] = 7```

Array constants cannot contain file-type values at any level.

Record Constants

To declare a record constant, specify the value of each field - as fieldName: value, with the field assignments separated by semicolons - in parentheses at the end of the declaration. The values must be represented by constant expressions. The fields must be listed in the order in which they appear in the record type declaration, and the tag field, if there is one, must have a value specified; if the record has a variant part, only the variant selected by the tag field can be assigned values.

Examples:

```type
TPoint = record
X, Y: Single;
end;
TVector = array[0..1] of TPoint;
TMonth = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
TDate = record
D: 1..31;
M: TMonth;
Y: 1900..1999;
end;
const
Origin: TPoint = (X: 0.0; Y: 0.0);
Line: TVector = ((X: -3.1; Y: 1.5), (X: 5.8; Y: 3.0));
SomeDay: TDate = (D: 2; M: Dec; Y: 1960);```

Record constants cannot contain file-type values at any level.

Procedural Constants

To declare a procedural constant, specify the name of a function or procedure that is compatible with the declared type of the constant. For example,

```function Calc(X, Y: Integer): Integer;
begin
...
end;

type TFunction = function(X, Y: Integer): Integer;
const MyFunction: TFunction = Calc;```

Given these declarations, you can use the procedural constant MyFunction in a function call:

`I := MyFunction(5, 7)`

You can also assign the value nil to a procedural constant.

Pointer Constants

When you declare a pointer constant, you must initialize it to a value that can be resolved at least as a relative address at compile time. There are three ways to do this: with the @ operator, with nil, and (if the constant is of type PChar or PWideChar) with a string literal. For example, if I is a global variable of type Integer, you can declare a constant like

`const PI: ^Integer = @I;`

The compiler can resolve this because global variables are part of the code segment. So are functions and global constants:

`const PF: Pointer = @MyFunction;`

Because string literals are allocated as global constants, you can initialize a PChar constant with a string literal:

`const WarningStr: PChar = 'Warning!';`

Writeable Typed Constants

Delphi allows typed constants to be modified if you set the compiler directive (\$J+} or {\$WRITEABLECONST ON}.

With \$J+ set, you can use assignment statements to change the value of typed constants as if they were initialized variables. For example:

```       const
foo: Integer = 12;
begin
foo := 14;
end.```

A notable difference between writeable typed constants and initialized variables is that writeable typed constants can occur both globally and locally in procedure, functions and methods, whereas initialized variables are only available as global declarations. Initialized variables cause a compile-time error when attempted within procedures or methods.