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.

1047 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
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
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
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
220
votes
11 answers

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

Is it better in C++ to pass by value or pass by constant reference? I am wondering which is better practice. I realize that pass by constant reference should provide for better performance in the program because you are not making a copy of the…
Matt Pascoe
  • 7,711
  • 16
  • 40
  • 47
186
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
  • 98,643
  • 98
  • 284
  • 354
180
votes
7 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,355
  • 4
  • 20
  • 26
118
votes
6 answers

How do I pass the value (not the reference) of a JS variable to a function?

Here is a simplified version of something I'm trying to run: for (var i = 0; i < results.length; i++) { marker = results[i]; google.maps.event.addListener(marker, 'click', function() { change_selection(i); }); } but I'm…
ryan
  • 2,241
  • 3
  • 22
  • 28
115
votes
17 answers

C++ - passing references to std::shared_ptr or boost::shared_ptr

If I have a function that needs to work with a shared_ptr, wouldn't it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)? What are the possible bad side effects? I envision two possible cases: 1) inside the…
abigagli
  • 2,740
  • 3
  • 28
  • 32
1
2 3
69 70