Difference between revisions of "Const"

From Free Pascal wiki
Jump to navigationJump to search
(→‎Const Parameter: optimization)
(see also constref)
Line 14: Line 14:
 
== 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]].
 
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]].
 +
 +
== See Also ==
 +
[[Constref]]
  
 
[[Category:Constants]]
 
[[Category:Constants]]
 
[[category:Reserved words]]
 
[[category:Reserved words]]

Revision as of 01:33, 24 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:

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.

See Also

Constref