Difference between revisions of "Variable parameter"

From Free Pascal wiki
Jump to navigationJump to search
(Category:Pascal)
m (Fixed syntax highlighting; resolved template loop)
Line 1: Line 1:
{{MenuTranslate| page=Variable parameter}}
+
{{LanguageBar}}
  
 
'''Variable parameter''' (or Reference Parameter) is an ''input and output parameter''  
 
'''Variable parameter''' (or Reference Parameter) is an ''input and output parameter''  
Line 8: Line 8:
 
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".  
 
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 ==
  
=== XOR swap ===
+
<syntaxhighlight lang="pascal">
 
 
<syntaxhighlight>
 
  
 
   procedure XorSwap(  var i,j:integer );
 
   procedure XorSwap(  var i,j:integer );
Line 22: Line 21:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== OrderSmallBig ===
+
== OrderSmallBig ==
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
  
 
   procedure OrderSmallBig( var a,b:integer );
 
   procedure OrderSmallBig( var a,b:integer );

Revision as of 07:51, 27 December 2019

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

Variable parameter (or Reference Parameter) is an input and output parameter meaning it can be used for passing a value to a function or procedure, as well as to get back a value from a function or procedure. It is indicated by the use of the keyword 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

  procedure XorSwap(  var i,j:integer );
  begin
    i := i xor j ;
    j := i xor j ;
    i := i xor j ;
  end;

OrderSmallBig

  procedure OrderSmallBig( var a,b:integer );
  begin
    if a > b then XorSwap( a, b );
  end;

Read more