Difference between revisions of "Variable parameter"

From Free Pascal wiki
Jump to navigationJump to search
(New page: Variable parameter (or Reference Parameter) is input and output parameter meaning it can be used for passing a value to a function or a procedure as well as to ...)
 
m (→‎See also: update URL)
 
(16 intermediate revisions by 7 users not shown)
Line 1: Line 1:
Variable parameter (or Reference Parameter) is input and output parameter
+
{{LanguageBar}}
meaning it can be used for passing a value to a [[Function|function]] or a [[Procedure|procedure]]
 
as well as to get back a value from function or procedure. It indicated by the use
 
of the [[Keyword|keyword]] [[Var|var]] in front of the formal parameter.
 
  
Variable parameter given a new nickname. The method refers to the same variable and changes made will affect
+
A '''variable parameter''' is a [[Routine|routine]] parameter that is a [[Variable|variable]].
the actual 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.
  
 +
== Usage ==
  
=== XOR swap ===
+
A variable parameter is declared by preceding a formal parameter declaration with the [[Var|keyword <syntaxhighlight lang="pascal" inline>var</syntaxhighlight>]].
  
<delphi>
+
<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>
  
  procedure XorSwap(  var i,j:integer );
+
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.
  begin
+
After <syntaxhighlight lang="pascal" inline>procedure xorSwap</syntaxhighlight> has been called the variables ''at the call site'' will have changed.
    i := i xor j ;
+
[[Becomes|Assignments]] to variable parameters have an effect in the scope where the routine is called.
    j := i xor j ;
+
Thus, a variable parameter can be considered as an alias for the actual argument given by the calling routine.
    i := i xor j ;
+
When a routine changes the value of a variable parameter, it is actually changing the variable in the code that called the routine.
  end;
 
  
</delphi>
+
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]].
 +
{{Note|[[File|<syntaxhighlight lang="pascal" inline>file</syntaxhighlight>]] and [[Text|<syntaxhighlight lang="pascal" inline>text</syntaxhighlight>]] variables always have to be declared as variable parameters.}}
  
=== OrderSmallBig ===
+
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 [[@|<syntaxhighlight lang="delphi" inline>@</syntaxhighlight>-address-operator]] is available:
  
<delphi>
+
<syntaxhighlight lang="pascal" highlight="1,3">
 +
procedure printAddress(var x);
 +
begin
 +
write(sysBackTraceStr(@x));
 +
end;
 +
</syntaxhighlight>
  
  procedure OrderSmallBig( var a,b:integer );
+
== Implementation ==
  begin
 
    if a > b then XorSwap( a, b );
 
  end;
 
  
</delphi>
+
The specific implementation of variable parameters is only of concern for those who are programming (pure) assembler routines.
  
== Read more ==
+
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.
  
* [[doc:/ref/refsu48.html#x120-12700011.3.2| Variable parameter]]
+
{{Note|In order to pass an address to the routine, all parameters have to be addressable (by ''one'' address).
* [[Xor]]
+
Although a [[Property|<syntaxhighlight lang="delphi" inline>property</syntaxhighlight>]] 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.}}
* [[If]]
+
 
 +
== See also ==
 +
* [https://freepascal.org/docs-html/current/ref/refsu65.html § “Variable Parameters” in the FPC Reference Guide]
 +
* [[Out|<syntaxhighlight lang="delphi" inline>out</syntaxhighlight>]] designates a parameter as non-readable, only writable
 +
* [[Constref|<syntaxhighlight lang="delphi" inline>constRef</syntaxhighlight>]]
 +
 
 +
[[Category:Pascal]]

Latest revision as of 23:52, 23 February 2022

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

A variable parameter is a routine parameter that is a 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.

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 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. 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 arguments when calling the routine, never constants or expressions.

Light bulb  Note: file and text variables always have to be declared as variable parameters.

With GNU Pascal and Free Pascal, variable parameters may have no associated data type (FPC [internally] calls this “formal type”). Such parameters do not allow any operations on them, but typecasting has to be used. Only the @-address-operator is available:

procedure printAddress(var x);
begin
	write(sysBackTraceStr(@x));
end;

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.

Light bulb  Note: In order to pass an address to the routine, all parameters have to be addressable (by one address).

Although a 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.

See also