Difference between revisions of "Variable parameter"

From Lazarus wiki
Jump to navigationJump to search
(add more constraints)
m (Fix English in second sentence)
Line 2: Line 2:
  
 
A '''variable parameter''' is a [[Routine|routine]] parameter that is a [[Variable|variable]].
 
A '''variable parameter''' is a [[Routine|routine]] parameter that is a [[Variable|variable]].
Calling a routine with a variable parameter requires to specify a variable at the proper position.
+
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.
 
The variable will (temporarily) be in the scope of the routine through the parameter’s name.
  
 
== Usage ==
 
== Usage ==
 +
 
A variable parameter is declared by preceding a formal parameter declaration with the [[Var|keyword <syntaxhighlight lang="pascal" inline>var</syntaxhighlight>]].
 
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">
 
<syntaxhighlight lang="pascal" highlight="1">
 
procedure xorSwap(var left, right: integer);
 
procedure xorSwap(var left, right: integer);
Line 15: Line 17:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
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.
 
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 <syntaxhighlight lang="pascal" inline>procedure xorSwap</syntaxhighlight> has been called the variables ''at the call site'' will have changed.
 
After <syntaxhighlight lang="pascal" inline>procedure xorSwap</syntaxhighlight> has been called the variables ''at the call site'' will have changed.

Revision as of 01:47, 30 October 2020

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

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

With GNU Pascal and FreePascal, variable parameters may have no associated data type (FPC [internally] calls this “formal type”). Such parameters do not allow any operations on it, 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 readable and writable, it does not have a single address to one memory block, thus it cannot be used as a variable parameter.

See also