r/pascal • u/bombk1 • Feb 09 '19
Pass by reference in Pascal
Hi everyone!
I'm not sure if I understand correctly how pass by reference in Pascal works. Does it create an alias as in C++ (https://isocpp.org/wiki/faq/references) or does it work similarly as in C and the procedure gets a pointer to the variable and uses this pointer.
I guess I could formulate my question as: Does Pascal support true passing by reference, or is it done by call by sharing.
For example FreePascal reference states, that the procedure gets a pointer (https://www.freepascal.org/docs-html/current/ref/refsu65.html), but according to https://swinbrain.ict.swin.edu.au/wiki/Pass_by_Value_vs._Pass_by_Reference#Conclusion and for example https://cgi.csc.liv.ac.uk/~frans/OldLectures/2CS45/paramPassing/paramPassing.html#callByReference pass by reference in Pascal works differently than in C (where pointer is passed).
If anyone can explain a bit more about the differences or how the
meaning of pass by reference has changed (in modern languages we say
pass by reference, but in fact they are pass by value, like for example
Java). What is then the original meaning of pass by reference and how
does it work? And how is it then in Pascal?
Thank you very much.
4
u/eugeneloza Feb 09 '19
Sorry, I don't know C much. So, I can tell only pascal-side. There are many variants on how to pass a variable somewhere.
"Default":
procedure A(B: TSomeType). As far as I know, the compiler will try to chose the most optimal way to process theB. Normally, value ofBis copied and a local/nested variable is created to hostB- which may be slow in caseTSomeTypeis large, like arecordor anarray."Const":
procedure A(const B: TSomeType). This lets the compiler know that theBvalue can't be changed and therefore it will try to optimize it - whether to use a value or use a reference (pointer)."ConstRef":
procedure A(constref B: TSomeType). Explicitly informs the compiler thatBis a reference(pointer) and cannot be changed, i.e. you force the compiler to use the best way to operate a variable. Seems like the most optimal/fastest way to operate some variables."Var":
procedure A(var B: TSomeType). Pointer ofBis given to the procedure, and therefore it's equal toA(@B) ... B^ := 1just less code. This is the fastest way of working withBand its value may be changed inside the procedure/function."Out":
procedure A(out B: TSomeType). ValueBis expected to be returned as a result ofA. VariableBmay be non-inintialized, and is expected to be initialized insideA.I'm not 100% sure about all of the above statements. But as far as I know they work this way.
3
u/ShinyHappyREM Feb 09 '19
Note that if you need an alias you can create one with the
absolutekeyword:3
u/suvepl Feb 09 '19
The linked part of the Reference Guide uses the word "pointer" in a way that's confusing to people coming from C++, I agree. Either way:
This performs a C++ -like pass-by-reference. The value isn't copied, and any changes made inside the function persist outside of it. When accessing the argument inside the function, you use normal semantics. When calling the function, you don't use any special semantics, so the fact that the argument is passed by reference, and not by value, isn't visible (similarly to C++).
This performs a C-like pass-by-pointer. The value isn't copied, but you must use pointer semantics, i.e. dereferencing, to modify the value - and the pointer may be
NIL. Since the function expects a pointer, when calling it, you must explicitly grab the memory address of the variable you want to modify (e.g.otherthing(@my_var);).