Difference between revisions of "Constref"

From Lazarus wiki
Jump to navigationJump to search
m
m
Line 2: Line 2:
  
 
Version 2.6 of Free Pascal added the '''constref''' parameter qualifier.
 
Version 2.6 of Free Pascal added the '''constref''' parameter qualifier.
 +
<source lang="Pascal">
 +
procedure test(constref l: longint);
 +
begin
 +
  writeln('This parameter has been passed by reference, although the Pascal code does not really care about that: ',l);
 +
end;
 +
 +
begin
 +
  test(5);
 +
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.  
 
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.  

Revision as of 12:53, 30 October 2017

English (en) français (fr)

Version 2.6 of Free Pascal added the constref parameter qualifier.

procedure test(constref l: longint);
begin
  writeln('This parameter has been passed by reference, although the Pascal code does not really care about that: ',l);
end;

begin
  test(5);
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.

This means that not only the parameter, but also the variable passed by the caller (e.g. a global var) is not 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 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