Questions tagged [pass-by-reference]

Pass by reference is an argument marshalling strategy whereby a variable's location in memory is passed to a function, rather than a copy of the variable's value, although the function appears in the source code to receive the variable itself rather than a pointer to it.

Passing by reference means that the memory address of a variable is passed rather than a copy of the variable's value.

This typically means that the function can modify the passed variable, assigning a new value to it. However for performance reasons, passing by reference may be useful even if the passed structure is not modified, as with the Pascal var modifier, and some programming languages have constructs (like the C const modifier) to disallow modification of a variable passed by reference.

4485 questions
42
votes
8 answers

Passing ArrayList as value only and not reference

Simply put, I have a method with an ArrayList parameter. In the method I modify the contents of the ArrayList for purposes relevant only to what is returned by the method. Therefore, I do not want the ArrayList which is being passed as the parameter…
fvgs
  • 21,412
  • 9
  • 33
  • 48
41
votes
1 answer

Passing objects and a list of objects by reference in C#

I have a delegate that modifies an object. I pass an object to the delegate from a calling method, however the calling method does not pick up these changes. The same code works if I pass a List as the object. I thought all objects were passed by…
David
  • 15,150
  • 15
  • 61
  • 83
40
votes
8 answers

Changing value after it's placed in HashMap changes what's inside HashMap?

If I create a new HashMap and a new List, and then place the List inside the Hashmap with some arbitrary key and then later call List.clear() will it affect what I've placed inside the HashMap? The deeper question here being: When I add something…
Diego
  • 16,830
  • 8
  • 34
  • 46
39
votes
6 answers

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

My code : locs = [ [1], [2] ] for loc in locs: loc = [] print locs # prints => [ [1], [2] ] Why is loc not reference of elements of locs ? Python : Everything is passed as reference unless explicitly copied [ Is this not True ? ] Please…
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
39
votes
1 answer

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

The phrase "pass by reference" is used by C and C++ developers alike but they appear to be used to mean different things. What exactly is the difference between this equivocal phrase in each language?
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
36
votes
6 answers

Why can't arrays be passed as function arguments?

Why can't you pass arrays as function arguments? I have been reading this C++ book that says 'you can't pass arrays as function arguments', but it never explains why. Also, when I looked it up online I found comments like 'why would you do that…
Hudson Worden
  • 2,263
  • 8
  • 30
  • 45
36
votes
4 answers

Modifying const reference argument via non-const reference argument

Consider the following code: #include void f(int const& a, int& b) { b = a+1; } int main() { int c=2; f(c,c); std::cout << c << std::endl; } Function f takes two reference arguments: int const& a and int& b. Therefore, f is…
36
votes
5 answers

When to pass-by-reference in PHP

Im wondering if its good practice to pass-by-reference when you are only reading a variable, or if it should always be passed as a value. Example with pass-by-reference: $a = 'fish and chips'; $b = do_my_hash($a); echo $b; function…
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
35
votes
3 answers

Is it possible to pass properties as "out" or "ref" parameters?

Can I pass a property as an "out" or "ref" parameter if not then why not? e.g. Person p = new Person(); . . . public void Test(out p.Name);
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135
35
votes
7 answers

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

Suppose I have a function that takes an argument of type T. It does not mutate it, so I have the choice of passing it by const reference const T& or by value T: void foo(T t){ ... } void foo(const T& t){ ... } Is there a rule of thumb of how big T…
gexicide
  • 38,535
  • 21
  • 92
  • 152
34
votes
6 answers

Function Overloading Based on Value vs. Const Reference

Does declaring something like the following void foo(int x) { std::cout << "foo(int)" << std::endl; } void foo(const int &x) { std::cout << "foo(const int &)" << std::endl; } ever make sense? How would the caller be able to…
kirakun
  • 2,770
  • 1
  • 25
  • 41
33
votes
4 answers

Java pass by reference

What is the difference between this 2 codes: Code A: Foo myFoo; myFoo = createfoo(); where public Foo createFoo() { Foo foo = new Foo(); return foo; } Vs. Code B: Foo myFoo; createFoo(myFoo); public void createFoo(Foo foo) { Foo f = new…
delita
  • 1,571
  • 1
  • 19
  • 25
33
votes
5 answers

STL containers with reference to objects

I know STL containers copy the objects. So say I have a list l; each time when I do SampleClass t(...); l.push_back(t); a copy of t will be made. If SampleClass is large, then it will be very costly. But if I declare l as a…
CuriousMind
  • 15,168
  • 20
  • 82
  • 120
33
votes
8 answers

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

In what circumstances should I prefer pass-by-reference? Pass-by-value?
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
33
votes
7 answers

javascript return reference to array item

I have a array like this: users = [{id:1, name:'name1'},{id:2, name:'name2'}] How could I get a reference to the item {id:2, name:'name2'}, so I can change it is name property, like: user = get_item(users, 'id', 2); user.name = "user2 name…
fuyi
  • 2,573
  • 4
  • 23
  • 46