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
8
votes
15 answers

Can someone explain to me what the reasoning behind passing by "value" and not by "reference" in Java is?

I'm fairly new to Java (been writing other stuff for many years) and unless I'm missing something (and I'm happy to be wrong here) the following is a fatal flaw... String foo = new String(); thisDoesntWork(foo); System.out.println(foo);//this prints…
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
8
votes
2 answers

Is it safe to pass const reference to temporary/annonymous lambda into std::thread constructor?

Following on from this question: can-a-temperary-lambda-by-passed-by-reference? I have a fixed code snippet: // global variable std::thread worker_thread; // Template function template void start_work(const Functor &worker_fn) …
code_fodder
  • 15,263
  • 17
  • 90
  • 167
8
votes
2 answers

Why a different type variable can be used as an argument of a parameter of const reference parameter in C++

void foo(const int& v) { int x = v; std::cout << x; } int main() { unsigned y = 1; foo(y); } Is passing y in place of a const int& legal in C++
xiaofeng z
  • 93
  • 4
8
votes
3 answers

When is foreach with a parameter by reference dangerous?

I knew, that it can be dangerous to pass the items by reference in foreach. In particular, one must not reuse the variable that was passed by reference, because it affects the $array, like in this example: $array = ['test']; foreach ($array as…
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
8
votes
2 answers

The efficiency when using a big data structure in a function in Python

I need to use a big data structure, more specifically, a big dictionary to do the looking up job. At the very first my code is like this: #build the dictionary blablabla #look up some information in the ditionary blablabla As I need to look up…
ibread
  • 1,165
  • 1
  • 10
  • 18
8
votes
4 answers

Recursion and passing by reference

I have a tree of categories of the following structure: [6] => Array ( [id] => 6 [name] => computers [productCount] => 0 [children] => Array ( [91] => Array ( …
sevenWonders
  • 175
  • 1
  • 9
8
votes
3 answers

Is it undefined behavior to pass-by-reference uninitialized variable?

I have the following code: #include void f(int &x) { x = 5; } int main() { int x; f(x); std::cout << x << std::endl; return 0; } Does this code invoke undefined behavior in C++? g++ compiles it without any warnings…
Andrew Sun
  • 4,101
  • 6
  • 36
  • 53
8
votes
3 answers

Boxing value type to send it to a method and get the result

I'm curious about how C# behaves when passing value/reference types into a method. I'd like to pass a boxed value type into the method "AddThree". The idea is to get in the caller function (Main) the result of the operations performed within…
Rober
  • 726
  • 8
  • 27
8
votes
1 answer

R: Pass data.frame by reference to a function

I pass a data.frame as parameter to a function that want to alter the data inside: x <- data.frame(value=c(1,2,3,4)) f <- function(d){ for(i in 1:nrow(d)) { if(d$value[i] %% 2 == 0){ d$value[i] <-0 } } print(d) } When I execute…
vtortola
  • 34,709
  • 29
  • 161
  • 263
8
votes
5 answers

Best Practice for Returning Object References

Consider this code snippet: class MyClass{ private List myList; //... public List getList(){ return myList; } } As Java passes object references by value, my understanding is that any object calling getList() will obtain a…
chrisbunney
  • 5,819
  • 7
  • 48
  • 67
8
votes
2 answers

Is function overloading by reference allowed when there is no ambiguity?

Consider following code: #include void foo(int m); void foo(int &k); int main() { foo(5); // ok, because there is no ambiguity int m = 5; //foo(m); // compile-time error, because of ambiguity foo(m + 0); // ok, because…
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
8
votes
4 answers

Pass int by const reference or by value , any difference?

When I pass primitives like int and double to functions , is it better to pass them by const reference , or by value (assuming that I don't change the variable's value) ? int getValueFromArray(int index) { // return the value from the…
JAN
  • 21,236
  • 66
  • 181
  • 318
8
votes
4 answers

What (are there any) languages with only pass-by-reference?

I was wondering. Are there languages that use only pass-by-reference as their eval strategy?
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
8
votes
4 answers

Passing an Object by reference to Overloaded Operator - C++

Quite new to C++. I have seen people usually pass objects by reference in operator overloading. Well, I can't figure out when it is really necessary. As in the code below, if I remove ampersand in declaration of object c1 and c2 in operator+, still…
QuestionMark
  • 412
  • 1
  • 4
  • 16
8
votes
1 answer

Why does passing a variable by reference not work when invoking a reflective method?

My function, prepare(), has the definition: private function prepare(&$data, $conditions=null, $conditionsRequired=false) When I test it, this /** * @covers /data/DB_Service::prepare * @uses /inc/config */ public function…
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128