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.

4024 questions
99
votes
6 answers

python pandas dataframe, is it pass-by-value or pass-by-reference

If I pass a dataframe to a function and modify it inside the function, is it pass-by-value or pass-by-reference? I run the following code a = pd.DataFrame({'a':[1,2], 'b':[3,4]}) def letgo(df): df = df.drop('b',axis=1) letgo(a) the value of a…
nos
  • 16,305
  • 22
  • 81
  • 113
92
votes
3 answers

How can I pass a reference to a function, with parameters?

Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application) I need to able to pass a reference to a function with a given set of parameters. Here is an example of passing a reference without…
Andreas Grech
  • 98,643
  • 98
  • 284
  • 354
84
votes
7 answers

Java : Best way to pass int by reference

I have a parsing function that parses an encoded length from a byte buffer, it returns the parsed length as an int, and takes an index into the buffer as an integer arg. I want the function to update the index according to what it's parsed, i.e.…
fred basset
  • 9,058
  • 26
  • 82
  • 131
83
votes
7 answers

Which is more efficient: Return a value vs. Pass by reference?

I am currently studying how to write efficient C++ code, and on the matter of function calls, a question comes to mind. Comparing this pseudocode function: not-void function-name () { do-something return value; } int main () { ... …
thegreatjedi
  • 2,438
  • 3
  • 18
  • 37
79
votes
7 answers

C++ pass an array by reference

is this allowed to pass an array by reference ? void foo(double& *bar) Seems that my compiler says no. Why? What is the proper way to pass an array by reference? Or a work around? I have an array argument that my method should modify and that I…
kiriloff
  • 22,522
  • 32
  • 127
  • 207
77
votes
5 answers

Difference between function arguments declared with & and * in C++

I typed the following example: #include double f(double* x, double* y) { std::cout << "val x: " << *x << "\n"; std::cout << "val y: " << *y << "\n"; return *x * *y; } double f2(double &x, double &y) { std::cout << "val x:…
tim
  • 8,782
  • 15
  • 70
  • 127
77
votes
2 answers

What exactly is copy-on-modify semantics in R, and where is the canonical source?

Every once in a while I come across the notion that R has copy-on-modify semantics, for example in Hadley's devtools wiki. Most R objects have copy-on-modify semantics, so modifying a function argument does not change the original value I can…
Andrie
  • 163,419
  • 39
  • 422
  • 472
73
votes
7 answers

Passing an array by reference in C?

How can I pass an array of structs by reference in C? As an example: struct Coordinate { int X; int Y; }; SomeMethod(Coordinate *Coordinates[]){ //Do Something with the array } int main(){ Coordinate Coordinates[10]; …
Hannoun Yassir
  • 18,630
  • 22
  • 74
  • 109
72
votes
8 answers

Passing values in Python

When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
72
votes
4 answers

Passing object by reference to std::thread in C++11

Why can't you pass an object by reference when creating a std::thread ? For example the following snippit gives a compile error: #include #include using namespace std; static void SimpleThread(int& a) // compile error //static…
austinmarton
  • 1,788
  • 2
  • 16
  • 21
71
votes
9 answers

In PHP (>= 5.0), is passing by reference faster?

In PHP, function parameters can be passed by reference by prepending an ampersand to the parameter in the function declaration, like so: function foo(&$bar) { // ... } Now, I am aware that this is not designed to improve performance, but to…
Hanno Fietz
  • 28,803
  • 43
  • 134
  • 232
70
votes
8 answers

C++ view types: pass by const& or by value?

This came up in a code review discussion recently, but without a satisfactory conclusion. The types in question are analogues to the C++ string_view TS. They are simple non-owning wrappers around a pointer and a length, decorated with some custom…
acm
  • 11,033
  • 5
  • 31
  • 60
69
votes
9 answers

Can you pass-by-reference in R?

Can you pass by reference with "R" ? for example, in the following code: setClass("MyClass", representation( name="character" )) instance1 <-new("MyClass",name="Hello1") instance2 <-new("MyClass",name="Hello2") array =…
Pierre
  • 31,741
  • 29
  • 101
  • 180
67
votes
9 answers

How can I pass an Integer class correctly by reference?

I am hoping that someone can clarify what is happening here for me. I dug around in the integer class for a bit but because integer is overriding the + operator I could not figure out what was going wrong. My problem is with this line: Integer i =…
sixtyfootersdude
  • 23,394
  • 39
  • 132
  • 200
66
votes
8 answers

Are pointers considered a method of calling by reference in C?

In my University's C programming class, the professor and subsequent book written by her uses the term call or pass by reference when referring to pointers in C. An example of what is considered a 'call by reference function' by my professor: int…
Insane
  • 893
  • 10
  • 21