Delphi procedure declaration var

Posted by Johan P14205 on May 18, 2006

The keyword "var" in the declaration of a Delphi procedure means that I send a "reference" to a variable, not only the value of the variable. For example:
S  := 'abc'; 
DoSomething(S); // (1)
ShowMessage(S); // (2)
...
 
procedure DoSomething (var Param: string);
 begin
  Param := Param + 'def';
  ...  

After calling DoSomething in line (1), S is changed. Line (2) shows 'abcde'. But S would stay the same if DoSomething is defined without "var", such as:

procedure DoSomething (Param: string);
begin
  Param := Param + 'def';
  ... 

So far, so good. But there seem to be exceptions on this rule: what if I don't use "VAR" and Param is a record, or an object, or an array? When is VAR needed?

Related Articles and Replies