Difference between revisions of "Constref"

From Free Pascal wiki
Jump to navigationJump to search
(Create)
 
 
(15 intermediate revisions by 6 users not shown)
Line 1: Line 1:
Version 2.6 of Free Pascal added the '''constref''' parameter qualifier. It is like a combination of the [[Var|var]] and [[Const | const]] parameter qualifiers. The argument is passed by reference, but cannot be modified. If you do try and modify the parameter, the compiler will flag it as an error: " Error: Can't assign values to const variable". The [[FPC_New_Features_2.6.0#Constref_parameter_modifier | new feature notes]] for version 2.6 suggest that you should only use this for interfacing with external routines in other languages, where this type of parameter passing is required.
+
{{Constref}}
  
 +
Version 2.6 of Free Pascal added the '''constref''' parameter qualifier.
 +
<source lang="Pascal">
 +
program call_method;
  
[[category:Reserved words]]
+
var x : longint ;
 +
 
 +
procedure test_cr(constref cr: longint);
 +
begin
 +
  writeln('Parameter 5 has been passed by reference: ', cr );
 +
end;
 +
 
 +
begin
 +
  test_cr(5);
 +
  x:=6;
 +
  test_cr(x);
 +
end.
 +
</source>
 +
 
 +
It is like the [[Const | const]] parameter qualifier. This qualifier informs the compiler that within the entire program there is no code that will change the value of the parameter while the procedure/function is executing.
 +
 
 +
This means that not only the ''parameter'', but also the ''variable'' (in above example: x) passed by the caller (e.g. a global var) must not be changed until the call with the constref parameter has returned.
 +
 
 +
In addition to being like [[Const | const]] parameter qualifier, the '''constref''' qualifier enforces that the parameter is passed by reference.
 +
 
 +
This differs from the [[Const | const]] parameters, which may be passed as reference or value depending on what the compiler thinks is best.
 +
 
 +
The [[FPC_New_Features_2.6.0#Constref_parameter_modifier | new feature notes]] for version 2.6 suggest that this can be used for interfacing with external routines in other languages, where this type of parameter passing is required.
 +
Other uses of constref may hinder the compiler from optimizing code.
 +
 
 +
== See also ==
 +
* [[Const|const]]
 +
* [[Var|var]]

Latest revision as of 10:47, 4 May 2019

English (en) français (fr)

Version 2.6 of Free Pascal added the constref parameter qualifier.

program call_method;

var x : longint ;

procedure test_cr(constref cr: longint);
begin
  writeln('Parameter 5 has been passed by reference: ', cr );
end;

begin
  test_cr(5);
  x:=6;
  test_cr(x);
end.

It is like the const parameter qualifier. This qualifier informs the compiler that within the entire program there is no code that will change the value of the parameter while the procedure/function is executing.

This means that not only the parameter, but also the variable (in above example: x) passed by the caller (e.g. a global var) must not be changed until the call with the constref parameter has returned.

In addition to being like const parameter qualifier, the constref qualifier enforces that the parameter is passed by reference.

This differs from the const parameters, which may be passed as reference or value depending on what the compiler thinks is best.

The new feature notes for version 2.6 suggest that this can be used for interfacing with external routines in other languages, where this type of parameter passing is required. Other uses of constref may hinder the compiler from optimizing code.

See also