Where should I prefer pass-by-reference or pass-by-value?

There are four main cases where you should use pass-by-reference over pass-by-value: If you are calling a function that needs to modify its arguments, use pass-by-reference or pass-by-pointer. Otherwise, you’ll get a copy of the argument. If you’re calling a function that needs to take a large object as a parameter, pass it by const … Read more

Java pass by reference

Java always passes arguments by value NOT by reference. Let me explain this through an example: public class Main { public static void main(String[] args) { Foo f = new Foo(“f”); changeReference(f); // It won’t change the reference! modifyReference(f); // It will modify the object that the reference variable “f” refers to! } public static … Read more

Function Overloading Based on Value vs. Const Reference

The intent seems to be to differenciate between invocations with temporaries (i.e. 9) and ‘regular’ argument passing. The first case may allow the function implementation to employ optimizations since it is clear that the arguments will be disposed afterwards (which is absolutely senseless for integer literals, but may make sense for user-defined objects). However, the … Read more

Rule of thumb for when passing by value is faster than passing by const reference?

If you have reason to suspect there is a worthwhile performance gain to be had, cut it out with the rules of thumb and measure. The purpose of the advise you quote is that you don’t copy great amounts of data for no reason, but don’t jeopardize optimizations by making everything a reference either. If … Read more

What exactly is the difference between “pass by reference” in C and in C++?

There are questions that already deal with the difference between passing by reference and passing by value. In essence, passing an argument by value to a function means that the function will have its own copy of the argument – its value is copied. Modifying that copy will not modify the original object. However, when … Read more

Python : When is a variable passed by reference and when by value? [duplicate]

Effbot (aka Fredrik Lundh) has described Python’s variable passing style as call-by-object: http://effbot.org/zone/call-by-object.htm Objects are allocated on the heap and pointers to them can be passed around anywhere. When you make an assignment such as x = 1000, a dictionary entry is created that maps the string “x” in the current namespace to a pointer … Read more

Are structs ‘pass-by-value’?

It is important to realise that everything in C# is passed by value, unless you specify ref or out in the signature. What makes value types (and hence structs) different from reference types is that a value type is accessed directly, while a reference type is accessed via its reference. If you pass a reference … Read more