Constref: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Constref
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Version 2.6 of Free Pascal added the **constref** parameter qualifier. | |
| 2 | + | ||
| 3 | + | ``` Pascal | |
| 4 | + | program call_method; | |
| 5 | + | ||
| 6 | + | var x : longint ; | |
| 7 | + | ||
| 8 | + | procedure test_cr(constref cr: longint); | |
| 9 | + | begin | |
| 10 | + | writeln('Parameter 5 has been passed by reference: ', cr ); | |
| 11 | + | end; | |
| 12 | + | ||
| 13 | + | begin | |
| 14 | + | test_cr(5); | |
| 15 | + | x:=6; | |
| 16 | + | test_cr(x); | |
| 17 | + | end. | |
| 18 | + | ``` | |
| 19 | + | ||
| 20 | + | 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. | |
| 21 | + | ||
| 22 | + | 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. | |
| 23 | + | ||
| 24 | + | In addition to being like [[Const|const]] parameter qualifier, the **constref** qualifier enforces that the parameter is passed by reference. | |
| 25 | + | ||
| 26 | + | This differs from the [[Const|const]] parameters, which may be passed as reference or value depending on what the compiler thinks is best. | |
| 27 | + | ||
| 28 | + | The [[FPC New Features 2.6.0|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. | |
| 29 | + | ||
| 30 | + | ## See also | |
| 31 | + | ||
| 32 | + | - [[Const|const]] | |
| 33 | + | - [[Var|var]] | |
| 34 | + | ||
| 35 | + | --- | |
| 36 | + | *From the Free Pascal wiki, page "Constref", as it stood on 2019-05-04 after a change by Jonas, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
