Posts tagged: functions

Pascal: parameters by reference

I posted some time ago about Pascal, and the nice (and worst) things that Pascal has in its language. I discovered yet another cool thing. Unlike C, where you can pass parameters to functions by value, or using a pointer (and therefore, as reference), in Pascal you can use a value, you can use a pointer, or declare a parameter as a variable. This is similar to passing by pointer, but with a cleaner syntax.

Note the difference between

  1. procedure foo(bar: integer);
  2.    begin
  3.        bar := 10;
  4.    end;

and

  1. procedure foo(var bar: integer);
  2.   begin
  3.      bar := 10;
  4.   end;

The first procedure does not change anything in the outside world, while the second changes the value of the variable passed as argument.

As expected, the compiler raises an error if you call this second procedure with a constant integer.