Difference between revisions of "Const"

From Free Pascal wiki
Jump to navigationJump to search
(Separate the two usages)
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{Const}}
 
{{Const}}
  
The '''const''' [[Keyword|keyword]] has two uses in a Pascal program:
+
The '''const''' [[Keyword|keyword]] has three uses in a [[Pascal]] [[Program|program]]:
* to start a constant declaration section
+
* to start a [[Constants|constant declaration section]]
* to declare ''const parameter'' for a function or procedure  
+
* to declare ''const parameter'' for a [[Function|function]] or [[Procedure|procedure]]
 +
* to declare a function with a variable number of parameters passed via a variable-sized array of different element types
  
 
== Const Section ==
 
== Const Section ==
The declaration '''const''' in a [[Pascal]] [[Program|program]] is used to inform the [[Compiler|compiler]] that certain [[Identifier|identifier]]s 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|var]]iable which is initialized at [[Run time|run time]].
+
The declaration '''const''' in a Pascal program is used to inform the [[Compiler|compiler]] that certain [[Identifier|identifier]]s 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|var]]iable which is initialized at [[runtime|run time]].
  
However, the default setting in 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 directives must be used to turn off the ability to assign to constant identifiers. That is {$J-}  or {$WriteableConst OFF} .
+
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 <syntaxhighlight lang="pascal" inline>{$J}</syntaxhighlight> (short form) or <syntaxhighlight lang="pascal" inline>{$WriteableConst}</syntaxhighlight> (long form) [[Compiler directive|compiler directives]] must be used to turn off the ability to assign to constant identifiers. That is <syntaxhighlight lang="pascal" inline>{$J-}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$WriteableConst OFF}</syntaxhighlight>.
  
 
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.
 
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.
 +
 
== Const Parameter ==
 
== Const Parameter ==
 +
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]].
 +
 +
== Array of Const ==
 +
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:
 +
 +
<syntaxhighlight lang="pascal">
 +
function MyFunction( array of const ) : Boolean;
 +
  ...
 +
 +
MyFunction( [10, 'global', True, my_var] );
 +
</syntaxhighlight>
 +
 +
This feature is only available in [[Compiler Mode|compiler mode]] [[Mode ObjFPC|ObjFPC]] or [[Mode Delphi|Delphi]].
 +
 +
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.
 +
 +
== See also ==
  
 +
* [[Constref]]
 +
* [[Constant]]
 +
* [[Constants]]
 +
* [[Class constants]]
  
 
[[Category:Constants]]
 
[[Category:Constants]]
[[category:Pascal]]
+
[[category:Reserved words]]

Revision as of 16:12, 6 August 2022

Deutsch (de) English (en) español (es) suomi (fi) français (fr) 中文(中国大陆)‎ (zh_CN)

The const keyword has three uses in a Pascal program:

Const Section

The declaration const in a Pascal program is used to inform the compiler that certain identifiers which are being declared are constants, that is, they are initialized with a specific value at compile time as opposed to a variable which is initialized at run time.

However, the default setting in 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 directives must be used to turn off the ability to assign to constant identifiers. That is {$J-} or {$WriteableConst OFF}.

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.

Const Parameter

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.

Array of Const

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:

function MyFunction( array of const ) : Boolean;
  ...

MyFunction( [10, 'global', True, my_var] );

This feature is only available in compiler mode ObjFPC or Delphi.

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.

See also