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
280
votes
7 answers

Are there benefits of passing by pointer over passing by reference in C++?

What are the benefits of passing by pointer over passing by reference in C++? Lately, I have seen a number of examples that chose passing function arguments by pointers instead of passing by reference. Are there benefits to doing…
Matt Pascoe
  • 8,651
  • 17
  • 42
  • 48
274
votes
8 answers

How to pass objects to functions in C++?

I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++. Do I need to pass pointers, references, or non-pointer and non-reference values? I remember in Java there are no such issues…
Rakesh K
  • 8,237
  • 18
  • 51
  • 64
257
votes
19 answers

Passing by reference in C

If C does not support passing a variable by reference, why does this work? #include void f(int *j) { (*j)++; } int main() { int i = 20; int *p = &i; f(p); printf("i = %d\n", i); return 0; } Output: $ gcc -std=c99 test.c $…
aks
  • 4,695
  • 8
  • 36
  • 37
242
votes
7 answers

Are arrays passed by value or passed by reference in Java?

Arrays are not a primitive type in Java, but they are not objects either, so are they passed by value or by reference? Does it depend on what the array contains, for example references or a primitive type?
Froskoy
  • 2,967
  • 5
  • 20
  • 21
238
votes
11 answers

Is it better in C++ to pass by value or pass by reference-to-const?

Is it better in C++ to pass by value or pass by reference-to-const? I am wondering which is better practice. I realize that pass by reference-to-const should provide for better performance in the program because you are not making a copy of the…
Matt Pascoe
  • 8,651
  • 17
  • 42
  • 48
209
votes
10 answers

What is the use of "ref" for reference-type variables in C#?

I understand that if I pass a value-type (int, struct, etc.) as a parameter (without the ref keyword), a copy of that variable is passed to the method, but if I use the ref keyword a reference to that variable is passed, not a new one. But with…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
203
votes
8 answers

Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?

class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3); } } Output: false class D { public static void main(String args[]) { Integer b2=127; …
vipin k.
  • 2,633
  • 5
  • 22
  • 27
191
votes
15 answers

Passing a String by Reference in Java?

I am used to doing the following in C: void main() { String zText = ""; fillString(zText); printf(zText); } void fillString(String zText) { zText += "foo"; } And the output is: foo However, in Java, this does not seem to work. I…
Soren Johnson
  • 2,561
  • 4
  • 21
  • 17
155
votes
1 answer

Modify a globally scoped variable inside of an anonymous function

I was playing around with anonymous functions in PHP and realized that they don't seem to reach variables outside of them. Is there any way to get around this problem? Example: $variable = "nothing"; functionName($someArgument, function() { …
einord
  • 2,278
  • 2
  • 20
  • 26
151
votes
9 answers

Swift: Pass array by reference?

I want to pass my Swift Array account.chats to chatsViewController.chats by reference (so that when I add a chat to account.chats, chatsViewController.chats still points to account.chats). I.e., I don't want Swift to separate the two arrays when the…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
145
votes
7 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
  • 19,875
  • 27
  • 98
  • 134
143
votes
17 answers

Default value to a parameter while passing by reference in C++

Is it possible to give a default value to a parameter of a function while we are passing the parameter by reference. in C++ For example, when I try to declare a function like: virtual const ULONG Write(ULONG &State = 0, bool sequence = true); When…
Sony
142
votes
8 answers

List passed by ref - help me explain this behaviour

Take a look at the following program: class Test { List myList = new List(); public void TestMethod() { myList.Add(100); myList.Add(50); myList.Add(10); ChangeList(myList); foreach…
coder_bro
  • 10,503
  • 13
  • 56
  • 88
137
votes
13 answers

Passing an integer by reference in Python

How can I pass an integer by reference in Python? I want to modify the value of a variable that I am passing to the function. I have read that everything in Python is pass by value, but there has to be an easy trick. For example, in Java you could…
CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216
136
votes
10 answers

Is Swift Pass By Value or Pass By Reference

I'm really new to Swift and I just read that classes are passed by reference and arrays/strings etc. are copied. Is the pass by reference the same way as in Objective-C or Java wherein you actually pass "a" reference or is it proper pass by…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99