Const: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Const
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The **const** [[Keyword|keyword]] has three uses in a [[Pascal]] [[Program|program]]: | |
| 2 | + | ||
| 3 | + | - to start a [[Constant|constant]] declaration section | |
| 4 | + | - to declare *const parameter* for a [[Function|function]] or [[Procedure|procedure]] | |
| 5 | + | - to declare a function with a variable number of parameters passed via a variable-sized array of different element types | |
| 6 | + | ||
| 7 | + | ## Const Section | |
| 8 | + | ||
| 9 | + | The declaration **const** in a Pascal program is used to inform the [[Compiler|compiler]] that certain [[Identifier|identifiers]] which are being declared are [[Constant|constants]], that is, they are initialized with a specific value at [[Compile time|compile time]] as opposed to a [[Var|variable]] which is initialized at [[runtime|run time]]. | |
| 10 | + | ||
| 11 | + | However, the default setting in [[FPC|Free Pascal]] is to allow const identifiers to be re-assigned to. In order to make them unchangeable, the `{$J}` (short form) or `{$WriteableConst}` (long form) [[Compiler directive|compiler directives]] must be used to turn off the ability to assign to constant identifiers. That is `{$J-}` or `{$WriteableConst OFF}`. | |
| 12 | + | ||
| 13 | + | In some Pascal compilers, the Const declaration is used to define variables which are initialized at compile time to a certain specific value, and that the variables so defined can change as the program executes. This can be used for initializing arrays at compile time as opposed to setting values when the program is executed. | |
| 14 | + | ||
| 15 | + | ## Const Parameter | |
| 16 | + | ||
| 17 | + | A function or procedure parameter may be declared **const**. Any assignment to a **const** parameter within a procedure or function and the compiler will flag it as an error: "Can't assign values to const variable". Declaring a parameter as const allows the compiler the possibility to do optimizations it couldn't do otherwise, such as passing by reference while retaining the semantics of passing by value. A const parameter cannot be passed to another function or procedure that requires a [[Variable parameter|variable parameter]]. | |
| 18 | + | ||
| 19 | + | ## Array of Const | |
| 20 | + | ||
| 21 | + | A function can declare a parameter as an **array of const**. This allows a a routine to effectively take a variable amount of different types of parameters, via a single variable-length array parameter. At run-time when the function/procedure is called, the actual elements of the array are turned into variant records of type [[TVarRec]]. The called routine can use High() to determine the number of elements in the array and look at the VType field of each TVarRec element, to determine the type it contains. Record types cannot be passed as part of an **array of const**, but simple types, classes and interfaces can be. You create an array of const by placing the components within \[\]. For example: | |
| 22 | + | ||
| 23 | + | ``` pascal | |
| 24 | + | function MyFunction( array of const ) : Boolean; | |
| 25 | + | ... | |
| 26 | + | ||
| 27 | + | MyFunction( [10, 'global', True, my_var] ); | |
| 28 | + | ``` | |
| 29 | + | ||
| 30 | + | This feature is only available in [[Compiler Mode|compiler mode]] [[Mode ObjFPC|ObjFPC]] or [[Mode Delphi|Delphi]]. | |
| 31 | + | ||
| 32 | + | Note: Due to Delphi compatibility, arrays of const can't have an unsigned 32-bit variable type. Trying to pass a Dword with the highest bit set will cause a range check error, or gets interpreted as a Longint. | |
| 33 | + | ||
| 34 | + | ## See also | |
| 35 | + | ||
| 36 | + | - [[Constref]] | |
| 37 | + | - [[Basic Pascal Tutorial/Chapter 1/Constants|Constants]] | |
| 38 | + | - [[Class constants]] | |
| 39 | + | ||
| 40 | + | --- | |
| 41 | + | *From the Free Pascal wiki, page "Const", as it stood on 2023-02-23 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 42 | + | Category: Constants | |
| 43 | + | Category: Reserved words |
