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);
  Temp: Integer;
  Temp:= A;
  A:= B;
  B:= Temp;

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

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: Logo

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

Facebook photo

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

Connecting to %s