Difference between revisions of "Variable parameter"

From Lazarus wiki
(Read more)
m (Text replace - "delphi>" to "syntaxhighlight>")
Line 12: Line 12:
 
=== XOR swap ===
 
=== XOR swap ===
  
<delphi>
+
<syntaxhighlight>
  
 
   procedure XorSwap(  var i,j:integer );
 
   procedure XorSwap(  var i,j:integer );
Line 21: Line 21:
 
   end;
 
   end;
  
</delphi>
+
</syntaxhighlight>
  
 
=== OrderSmallBig ===
 
=== OrderSmallBig ===
  
<delphi>
+
<syntaxhighlight>
  
 
   procedure OrderSmallBig( var a,b:integer );
 
   procedure OrderSmallBig( var a,b:integer );
Line 32: Line 32:
 
   end;  
 
   end;  
  
</delphi>
+
</syntaxhighlight>
  
 
== Read more ==
 
== Read more ==

Revision as of 16:06, 24 March 2012


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 get back a value from function or procedure. It indicated by the use of the keyword 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 the actual variable.


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