Difference between revisions of "Const"

From Free Pascal wiki
Jump to navigationJump to search
(Separate the two usages)
Line 1: Line 1:
 
{{Const}}
 
{{Const}}
  
 +
The '''const''' [[Keyword|keyword]] has two uses in a Pascal program:
 +
* to start a constant declaration section
 +
* to declare ''const parameter'' for a function or procedure
 +
 +
== 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|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]].
 +
 +
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.
 
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.
<br>
+
== Const Parameter ==
<br>
+
 
  
 
[[Category:Constants]]
 
[[Category:Constants]]
 
[[category:Pascal]]
 
[[category:Pascal]]

Revision as of 16:08, 22 July 2016

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

The const keyword has two uses in a Pascal program:

  • to start a constant declaration section
  • to declare const parameter for a function or procedure

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