How to call procedure/function by reference in Free Pascal

If you want to call a procedure/function with variables and you want that procedure/function to modify parameters value, add var keyword in procedure/function parameter declaration:


procedure SwapNum(var A, B: Integer);
var
  Temp: Integer;
begin
  Temp:= A;
  A:= B;
  B:= Temp;
end;

var
  x, y: Integer;
begin
  x:= 10;
  y:= 20;
  SwapNum(x, y);
  Writeln('X = ', x);
  Writeln('Y = ', y);
end.

This type of passing parameters is called calling by reference it requires a variable with the same exact type to by passed and it will not work if a constant or values with other types are passed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s