Difference between revisions of "Variable parameter"

From Lazarus wiki
Jump to navigationJump to search
(reword 2nd paragraph)
Line 1: Line 1:
 
{{MenuTranslate| page=Variable parameter}}
 
{{MenuTranslate| page=Variable parameter}}
  
Variable parameter (or Reference Parameter) is input and output parameter  
+
'''Variable parameter''' (or Reference Parameter) is an ''input and output parameter''
meaning it can be used for passing a value to a [[Function|function]] or a [[Procedure|procedure]]
+
meaning it can be used for passing a value to a [[Function|function]] or [[Procedure|procedure]],
as well as to get back a value from function or procedure. It indicated by the use
+
as well as to get back a value from a function or procedure. It is indicated by the use
 
of the [[Keyword|keyword]] [[Var|var]] in front of the formal parameter.
 
of the [[Keyword|keyword]] [[Var|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
+
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.
the actual variable.
 
  
  

Revision as of 12:19, 17 July 2016

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