Questions tagged [pass-by-value]

pass-by-value is a "one way passing" so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

pass-by-value is a one way passing so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

pass-by-value normally means cloning a variable and making a cloned copy available to the receiving function. Modifications of this copy are allowed, but they are not returned back to the caller; the copy is simply disposed after the call returns.

Passing by value is more secure if the function can also be called by malicious code (malicious code can retain the reference and observe it later even if it cannot modify it). Some are also convinced it results a cleaner code. However passing of the larger structures by value also requires more resources.

1173 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
2109
votes
40 answers

Copy array by value

When copying an array in JavaScript to another array: var arr1 = ['a','b','c']; var arr2 = arr1; arr2.push('d'); // Now, arr1 = ['a','b','c','d'] I realized that arr2 refers to the same array as arr1, rather than a new, independent array. How can…
Dan
  • 21,377
  • 5
  • 18
  • 16
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
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
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
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
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
1
2 3
78 79