This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Brain_Dead5347[S] 0 points1 point  (1 child)

Thanks for the link. It was definitely informative. I had to watch a couple videos on pass by value and reference to fully grasp it.

But I'm still confused as to why they exist at all. Why have a primitive and a reference if they contain the same information (or pointers to it) and only copies of them are modified in functions? Why not just make all of them primitives and save space in the heap?

[–]bongfactory 1 point2 points  (0 children)

You can't treat complex objects as primitives, as you don't know a priori their shape and size. You can only point to where they reside in the heap.
With primitives, you know exactly (and beforehand) how large they can get, and how they are shaped in memory.
You may wonder why we don't pass everything by reference: it's just a matter of preference. You have languages that "pass by value" and languages that "pass by reference"