Variable parameter

From Free Pascal wiki
Revision as of 11:19, 17 July 2016 by FPC user (talk | contribs) (reword 2nd paragraph)
Jump to navigationJump to search

Template:MenuTranslate

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 a nickname to 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.


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