Is pass-by-value/reference equivalent to making a deep/shallow copy, respectively?

Viewed 5664

To reword the question in case someone types it into the search bar differently: Is pass-by-value the same as making a deep copy, and is pass-by-reference the same as making a shallow copy?

If not, what is the difference? In Python, the language I'm most familiar with, they appear indistinguishable.

4 Answers

pass-by-value makes a shallow-copy of the object.

On the other side,

pass-by-reference does not make any copy, it gets the reference of the object itself by just renaming it, so no any copying operation.

Since I'm here anyway.. from the docs...

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

So its about how you copy an object.. which of course contains references to other objects.

Do you just copy the immediate object (copy), or recursively copy all objects it holds references too (deepcopy).

Related