Variable parameter

From Free Pascal wiki
Jump to: navigation, search

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