Variable parameter: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Variable parameter
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A **variable parameter** is a [[Routine|routine]] parameter that is a [[Variable|variable]]. To call a routine with a variable parameter, you need to specify a variable at the proper position. The variable will (temporarily) be in the scope of the routine through the parameter’s name. | |
| 2 | + | ||
| 3 | + | ## Usage | |
| 4 | + | ||
| 5 | + | A variable parameter is declared by preceding a formal parameter declaration with the [[Var|keyword var]]. | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | procedure xorSwap(var left, right: integer); | |
| 9 | + | begin | |
| 10 | + | left := left xor right; | |
| 11 | + | right := left xor right; | |
| 12 | + | left := left xor right; | |
| 13 | + | end; | |
| 14 | + | ``` | |
| 15 | + | ||
| 16 | + | Variable parameters can serve as both input and output, meaning they can be used for passing a value *to* a routine, *and* getting a value *from* it. After `procedure xorSwap` has been called the variables *at the call site* will have changed. [[Becomes|Assignments]] to variable parameters have an effect in the scope where the routine is called. Thus, a variable parameter can be considered as an alias for the actual argument given by the calling routine. When a routine changes the value of a variable parameter, it is actually changing the variable in the code that called the routine. | |
| 17 | + | ||
| 18 | + | Since a variable parameter may appear on the left hand side of an assignment, only *variables* may be supplied as arguments when calling the routine, never [[Constant|constants]] or [[expression|expressions]]. | |
| 19 | + | ||
| 20 | + | > **Note:** [[File|file]] and [[Text|text]] variables always have to be declared as variable parameters. | |
| 21 | + | ||
| 22 | + | With [[GNU Pascal]] and Free Pascal, variable parameters may have no associated [[Data type|data type]] (FPC \[internally\] calls this “formal type”). Such parameters do not allow any operations on them, but [[Typecast|typecasting]] has to be used. Only the [[@|@-address-operator]] is available: | |
| 23 | + | ||
| 24 | + | ``` pascal | |
| 25 | + | procedure printAddress(var x); | |
| 26 | + | begin | |
| 27 | + | write(sysBackTraceStr(@x)); | |
| 28 | + | end; | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | ## Implementation | |
| 32 | + | ||
| 33 | + | The specific implementation of variable parameters is only of concern for those who are programming (pure) assembler routines. | |
| 34 | + | ||
| 35 | + | In [[FPC]], variable parameters are implemented by passing a reference to the variable at the call site (call by reference). For this reason, variable parameters are also referred to as *reference parameters*. If a routine is [[Inline|inlined]], the extra level of indirection is eliminated. | |
| 36 | + | ||
| 37 | + | > **Note:** In order to pass an address to the routine, all parameters have to be addressable (by *one* address). Although a [[Property|property]] may be readable *and* writable, it does not have a single address in one memory block, thus it cannot be used as a variable parameter. | |
| 38 | + | ||
| 39 | + | ## See also | |
| 40 | + | ||
| 41 | + | - [§ “Variable Parameters” in the FPC Reference Guide](https://freepascal.org/docs-html/current/ref/refsu65.html) | |
| 42 | + | - [[Out|out]] designates a parameter as non-readable, only writable | |
| 43 | + | - [[Constref|constRef]] | |
| 44 | + | ||
| 45 | + | --- | |
| 46 | + | *From the Free Pascal wiki, page "Variable parameter", as it stood on 2022-02-23 after a change by Kai Burghardt, 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).* | |
| 47 | + | Category: Pascal |
