Difference between revisions of "Variable parameter"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting; resolved template loop)
(untangle)
Line 1: Line 1:
 
{{LanguageBar}}
 
{{LanguageBar}}
  
'''Variable parameter''' (or Reference Parameter) is an ''input and output parameter''
+
A variable parameter refers to a formal parameter the actual parameter has direct writable access to the [[Variable|variable]] supplied at the call site.
meaning it can be used for passing a value to a [[Function|function]] or [[Procedure|procedure]],
 
as well as to get back a value from a function or procedure. It is indicated by the use
 
of the [[Keyword|keyword]] [[Var|var]] in front of the formal parameter.
 
 
 
A variable parameter can be considered an alias for the actual argument given by the calling routine. When a procedure or function changes the value of a variable parameter they are actually changing the variable in the code that called the function or procedure. The use of variable parameters is referred to as "pass by reference", since the function or procedure is receiving a reference to the actual parameter. The alternative to variable parameters - ''value parameters'' - provide a copy of the value that was in the actual arguments. Using value parameters is referred to as "pass by value".  
 
 
 
== XOR swap ==
 
 
 
<syntaxhighlight lang="pascal">
 
 
 
  procedure XorSwap(  var i,j:integer );
 
  begin
 
    i := i xor j ;
 
    j := i xor j ;
 
    i := i xor j ;
 
  end;
 
  
 +
== Usage ==
 +
A variable parameter is declared by preceding a formal parameter declaration with the [[Var|keyword <syntaxhighlight lang="pascal" inline>var</syntaxhighlight>]].
 +
<syntaxhighlight lang="pascal" highlight="1">
 +
procedure xorSwap(var left, right: integer);
 +
begin
 +
left := left xor right;
 +
right := left xor right;
 +
left := left xor right;
 +
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
Variable parameters can serve as both input and output, meaning it can be used for passing a value ''to'' a [[Routine|routine]], ''and'' get a value ''from'' it.
 +
After <syntaxhighlight lang="pascal" inline>procedure xorSwap</syntaxhighlight> 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.
  
== OrderSmallBig ==
+
Since a variable parameter may appear on the left hand side of an assignment, only ''variables'' may be supplied as argument when calling the routine, never [[Constant|constants]] or [[expression|expressions]].
  
<syntaxhighlight lang="pascal">
+
== Implementation ==
 +
The specific implementation of variable parameters is only of concern for those who are programming (pure) assembler routines.
  
  procedure OrderSmallBig( var a,b:integer );
+
In [[FPC]], variable parameters are implemented by passing a reference to the variable at the call site (call by reference).
  begin
+
For this reason, variable parameters are also referred to as ''reference parameters''.
    if a > b then XorSwap( a, b );
+
If a routine is [[Inline|inlined]], the extra level of indirection is eliminated.
  end;
 
 
 
</syntaxhighlight>
 
  
== Read more ==
+
== See also ==
* [http://lazarus-ccr.sourceforge.net/fpcdoc/ref/refsu48.html Variable parameter]
+
* [https://freepascal.org/docs-html/current/ref/refsu64.html § “Variable Parameters” in the FPC Reference Guide]
* [[Xor|xor]]
+
* [[Out|<syntaxhighlight lang="delphi" inline>out</syntaxhighlight>]] designates a parameter as non-readable, only writable
* [[Var|var]]
+
* [[Constref|<syntaxhighlight lang="delphi" inline>constRef</syntaxhighlight>]]
* [[Out|out]]
 
* [[If|if]]
 
  
 
[[Category:Pascal]]
 
[[Category:Pascal]]

Revision as of 23:50, 28 October 2020

English (en) español (es) suomi (fi)

A variable parameter refers to a formal parameter the actual parameter has direct writable access to the variable supplied at the call site.

Usage

A variable parameter is declared by preceding a formal parameter declaration with the keyword var.

procedure xorSwap(var left, right: integer);
begin
	left := left xor right;
	right := left xor right;
	left := left xor right;
end;

Variable parameters can serve as both input and output, meaning it can be used for passing a value to a routine, and get a value from it. After procedure xorSwap has been called the variables at the call site will have changed. 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.

Since a variable parameter may appear on the left hand side of an assignment, only variables may be supplied as argument when calling the routine, never constants or expressions.

Implementation

The specific implementation of variable parameters is only of concern for those who are programming (pure) assembler routines.

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 inlined, the extra level of indirection is eliminated.

See also