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
7724
votes
91 answers

Is Java "pass-by-reference" or "pass-by-value"?

I always thought Java uses pass-by-reference. However, I read a blog post which claims that Java uses pass-by-value. I don't think I understand the distinction the author is making. What is the explanation?
user4315
  • 4,775
  • 5
  • 23
  • 9
3259
votes
40 answers

How do I pass a variable by reference?

I wrote this class for testing: class PassByReference: def __init__(self): self.variable = 'Original' self.change(self.variable) print(self.variable) def change(self, var): var = 'Changed' When I tried…
David Sykes
  • 48,469
  • 17
  • 71
  • 80
1742
votes
34 answers

Is JavaScript a pass-by-reference or pass-by-value language?

The primitive types (number, string, etc.) are passed by value, but objects are unknown, because they can be both passed-by-value (in which case we consider that a variable holding an object is in fact a reference to the object) and…
Danail Nachev
  • 19,231
  • 3
  • 21
  • 17
704
votes
18 answers

What's the difference between passing by reference vs. passing by value?

What does it mean to say that a parameter is passed "by reference" or "by value"? How do such parameters differ?
ritu
470
votes
13 answers

Does JavaScript pass by reference?

Does JavaScript pass by references or pass by values? Here is an example from JavaScript: The Good Parts. I am very confused about the my parameter for the rectangle function. It is actually undefined, and redefined inside the function. There are no…
J Any
  • 4,847
  • 3
  • 13
  • 8
462
votes
11 answers

change values in array when doing foreach

example: var arr = ["one","two","three"]; arr.forEach(function(part){ part = "four"; return "four"; }) alert(arr); The array is still with it's original values, is there any way to have writing access to array's elements from iterating…
rsk82
  • 28,217
  • 50
  • 150
  • 240
452
votes
12 answers

Why should I use the keyword "final" on a method parameter in Java?

I can't understand where the final keyword is really handy when it is used on method parameters. If we exclude the usage of anonymous classes, readability and intent declaration then it seems almost worthless to me. Enforcing that some data…
Yotam Gilboa
422
votes
4 answers

JavaScript by reference vs. by value

I'm looking for some good comprehensive reading material on when JavaScript passes something by value and when by reference and when modifying a passed item affects the value outside a function and when not. I'm also interested in when assigning to…
jfriend00
  • 683,504
  • 96
  • 985
  • 979
386
votes
16 answers

Pass variables by reference in JavaScript

How do I pass variables by reference in JavaScript? I have three variables that I want to perform several operations to, so I want to put them in a for loop and perform the operations to each one. Pseudocode: myArray = new Array(var1, var2,…
BFTrick
  • 5,211
  • 6
  • 24
  • 28
341
votes
11 answers

Why use the 'ref' keyword when passing an object?

If I am passing an object to a method, why should I use the ref keyword? Isn't this the default behaviour anyway? For example: class Program { static void Main(string[] args) { TestRef t = new TestRef(); t.Something =…
Ryan
  • 4,671
  • 4
  • 23
  • 21
325
votes
9 answers

Passing Objects By Reference or Value in C#

In C#, I have always thought that non-primitive variables were passed by reference and primitive values passed by value. So when passing to a method any non-primitive object, anything done to the object in the method would effect the object being…
Michael
  • 8,229
  • 20
  • 61
  • 113
313
votes
8 answers

Are arrays in PHP copied as value or as reference to new variables, and when passed to functions?

1) When an array is passed as an argument to a method or function, is it passed by reference, or by value? 2) When assigning an array to a variable, is the new variable a reference to the original array, or is it new copy? What about doing this: $a…
Frank
  • 18,432
  • 9
  • 30
  • 30
309
votes
16 answers

Are PHP Variables passed by value or by reference?

Are PHP variables passed by value or by reference?
cmcculloh
  • 47,596
  • 40
  • 105
  • 130
295
votes
16 answers

Passing properties by reference in C#

I'm trying to do do the following: GetString( inputString, ref Client.WorkPhone) private void GetString(string inValue, ref string outValue) { if (!string.IsNullOrEmpty(inValue)) { outValue = inValue; } } This is giving…
yogibear
  • 14,487
  • 9
  • 32
  • 31
282
votes
14 answers

Is Ruby pass by reference or by value?

@user.update_languages(params[:language][:language1], params[:language][:language2], params[:language][:language3]) lang_errors = @user.errors logger.debug…
Sid
  • 6,134
  • 9
  • 34
  • 57
1
2 3
99 100