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
7058
votes
89 answers

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

I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, this blog) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're…
user4315
  • 4,725
  • 5
  • 20
  • 9
2876
votes
29 answers

How do I pass a variable by reference?

The Python documentation seems unclear about whether parameters are passed by reference or value, and the following code produces the unchanged value 'Original' class PassByReference: def __init__(self): self.variable = 'Original' …
David Sykes
  • 43,314
  • 17
  • 65
  • 77
1521
votes
32 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 case we consider that a variable holding an object is in fact a reference to the object) and passed-by-reference…
Danail Nachev
  • 16,971
  • 3
  • 18
  • 17
613
votes
16 answers

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

What is the difference between a parameter passed by reference a parameter passed by value? Could you give me some examples, please?
ritu
418
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,327
  • 3
  • 11
  • 7
407
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
395
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
  • 580,699
  • 78
  • 809
  • 825
364
votes
10 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
  • 24,158
  • 43
  • 131
  • 223
315
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
  • 4,331
  • 5
  • 22
  • 28
312
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,331
  • 3
  • 21
  • 21
278
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
  • 15,722
  • 9
  • 28
  • 30
272
votes
16 answers

Are PHP Variables passed by value or by reference?

Are PHP variables passed by value or by reference?
cmcculloh
  • 43,791
  • 36
  • 94
  • 126
271
votes
7 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
  • 6,405
  • 14
  • 52
  • 98
260
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
  • 5,762
  • 7
  • 31
  • 52
257
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
  • 7,127
  • 16
  • 46
  • 60
1
2 3
99 100