A simpler answer than the (very good) other.
does it work similarly as in C and the procedure gets a pointer to the variable and uses this pointer.
Yes, that's what happens under the hood. The procedure gets really a pointer, an address, to the variable. But the compiler, knowing that, makes it transparent. So, inside a procedure which declares a parameter "a", the statement:
a := a div 2;
can be compiled in two different ways. If the a
parameter is declared normally, i.e. passed by value, the statement is compiled like:
1 - load the value at address "a"
2 - integer divide by two
3 - store the result at address "a"
If, instead, the parameter was declared as var
, meaning passed by reference, the compiler does:
1 - load the value at address "a"
2 - load the value at address just loaded (it's a pointer dereferencing)
3 - divide
4 - store back
The above four statements are exactly what C compiles if the source is:
*a = *a / 2;
I guess I could formulate my question as: Does Pascal support true passing by reference ...?
The answer is absolutely yes, true passing by reference, and not many languages do this so well and cleanly. The source code for calling a procedure does not change, whether the procedure is called with "by reference" or "by value". Again the compiler, knowing how the formal parameters are to be passed, hides the details, unlike C.
For example, take a call to a procedure which wants a parameter passed by value:
myproc(a); // pascal and C are identical
Things get different if the procedure expect a pass by reference:
myproc(a); // pascal: identical to before
myproc(&a); // C: the language does not hide anything
About this last point, someone thinks that C is best because it forces the programmer to know that the passed variable can be modified by the procedure (function). I think, instead, that pascal is more elegant, and that a programmer should know anyway what the procedure will do.
All this is for "simple" types. If we talk of strings (and modern pascal has two flavours of them), it's the same - in Pascal. The compiler copies, increments reference counts, does whatever is needed to have full support for passing by value or by reference. C language does not have something similar.
If we talk about classes, things are different, but they must differ anyway because of the semantics of classes.
I hope to have added something to the other complete answer.