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
55
votes
6 answers

Java is NEVER pass-by-reference, right?...right?

Possible Duplicate: Is Java “pass-by-reference”? I found an unusual Java method today: private void addShortenedName(ArrayList voiceSetList, String vsName) { if (null == vsName) vsName = ""; else vsName =…
Troy Nichols
  • 1,234
  • 1
  • 13
  • 17
53
votes
6 answers

Pass an array to a function by value

Below is a snippet from the book C Programming Just the FAQs. Isn't this wrong as Arrays can never be passed by value? VIII.6: How can you pass an array to a function by value? Answer: An array can be passed to a function by value by declaring in …
Zuzu
  • 3,363
  • 5
  • 27
  • 16
52
votes
11 answers

Pass by reference or pass by value?

When learning a new programming language, one of the possible roadblocks you might encounter is the question whether the language is, by default, pass-by-value or pass-by-reference. So here is my question to all of you, in your favorite language,…
sven
  • 18,198
  • 10
  • 51
  • 62
49
votes
8 answers

Pass by value faster than pass by reference

I made a simple program in c++ to compare performance between two approaches - pass by value and pass by reference. Actually pass by value performed better than pass by reference. The conclusion should be that passing by value require fewer…
Björn Hallström
  • 3,775
  • 9
  • 39
  • 50
47
votes
6 answers

Performance cost of passing by value vs. by reference or by pointer?

Let's consider an object foo (which may be an int, a double, a custom struct, a class, whatever). My understanding is that passing foo by reference to a function (or just passing a pointer to foo) leads to higher performance since we avoid making a…
45
votes
9 answers

Are structs 'pass-by-value'?

I've recently tried to create a property for a Vector2 field, just to realize that it doesn't work as intended. public Vector2 Position { get; set; } this prevents me from changing the values of its members (X & Y) Looking up information on this, I…
Acidic
  • 6,154
  • 12
  • 46
  • 80
45
votes
6 answers

Is Java really passing objects by value?

Possible Duplicate: Is Java pass by reference? public class myClass{ public static void main(String[] args){ myObject obj = new myObject("myName"); changeName(obj); System.out.print(obj.getName()); // This prints…
44
votes
5 answers

Is passing pointer argument, pass by value in C++?

Is passing pointer argument, pass by value in C++? Since i see that any change to the pointer as such is not reflected outside the method. The changes i do by dereferencing the pointer is reflected though. In that case, is it acceptable/standard…
Sulla
  • 7,631
  • 9
  • 45
  • 71
43
votes
5 answers

Pass by reference and value in C++

I would like to clarify the differences between by value and by reference. I drew a picture: So, for passing by value, a copy of an identical object is created with a different reference, and the local variable is assigned the new reference, so to…
user36064
  • 843
  • 2
  • 10
  • 13
39
votes
6 answers

Python : When is a variable passed by reference and when by value?

My code : locs = [ [1], [2] ] for loc in locs: loc = [] print locs # prints => [ [1], [2] ] Why is loc not reference of elements of locs ? Python : Everything is passed as reference unless explicitly copied [ Is this not True ? ] Please…
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
39
votes
1 answer

What exactly is the difference between "pass by reference" in C and in C++?

The phrase "pass by reference" is used by C and C++ developers alike but they appear to be used to mean different things. What exactly is the difference between this equivocal phrase in each language?
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
35
votes
7 answers

Rule of thumb for when passing by value is faster than passing by const reference?

Suppose I have a function that takes an argument of type T. It does not mutate it, so I have the choice of passing it by const reference const T& or by value T: void foo(T t){ ... } void foo(const T& t){ ... } Is there a rule of thumb of how big T…
gexicide
  • 38,535
  • 21
  • 92
  • 152
34
votes
6 answers

Function Overloading Based on Value vs. Const Reference

Does declaring something like the following void foo(int x) { std::cout << "foo(int)" << std::endl; } void foo(const int &x) { std::cout << "foo(const int &)" << std::endl; } ever make sense? How would the caller be able to…
kirakun
  • 2,770
  • 1
  • 25
  • 41
33
votes
4 answers

Java pass by reference

What is the difference between this 2 codes: Code A: Foo myFoo; myFoo = createfoo(); where public Foo createFoo() { Foo foo = new Foo(); return foo; } Vs. Code B: Foo myFoo; createFoo(myFoo); public void createFoo(Foo foo) { Foo f = new…
delita
  • 1,571
  • 1
  • 19
  • 25
33
votes
8 answers

Where should I prefer pass-by-reference or pass-by-value?

In what circumstances should I prefer pass-by-reference? Pass-by-value?
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
1 2
3
78 79