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
0
votes
1 answer

Why does pass-by-value work in this example?

This example is taken from the Java programming book; 4th Edition. After stumbling upon this code example I am mystified at why it still prints out the new reference to the object, even though we declared it to be 'null'. In theory, we're altering…
0
votes
1 answer

Pass or no by reference?

I'm writing a MyClassCollection. It's an array,that will store a lot of instances of MyClass. The array is declared as the following: MyClass[] myClassInstances; that by Add(MyClass ins) method I store ins in myClassInstances. My question is:…
Jack
  • 16,276
  • 55
  • 159
  • 284
0
votes
2 answers

Java pass-by-value workaround for object properties

In one method of my Java source code, the same block of code occurs many times:
0
votes
1 answer

Javascript's objects added to array is pass by value or reference?

Possible Duplicate: Is JavaScript a pass-by-reference or pass-by-value language? In this file I have an object this.objectA and this.allAs; this.objectA contains a few attributes. Everytime I got a new this.objectA, I add it to the array…
yeeen
  • 4,911
  • 11
  • 52
  • 73
0
votes
1 answer

Passing an object by const reference?

In C++ when you want a function to be able to read from an object, but not modify it, you pass a const reference to the function. What is the equivalent way of doing this in php? I know objects in php5 are passed by reference by default, but for…
Nate
  • 26,164
  • 34
  • 130
  • 214
0
votes
4 answers

Passing an object by value and then by reference

I have the following code: static void Main(string[] args) { myclass c = new myclass(); c.test1 = 1; myclass c2 = TestPassByValByRef(c); Console.WriteLine("c.Test1: {0}", c.test1); …
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
0
votes
4 answers

Swapping function : Java Pass-By-Value code failed

public void swap(Point var1, Point var2) { var1.x = 100; var1.y = 100; Point temp = var1; var1 = var2; var2 = temp; } public static void main(String [] args) { Point p1 = new Point(0,0); Point p2 = new Point(0,0); …
bouncingHippo
  • 5,940
  • 21
  • 67
  • 107
0
votes
5 answers

What's the difference between passing argument by value and by reference?

Possible Duplicate: What's the difference between passing by reference vs. passing by value? I read that in C arguments are passed by value, but what's is the difference between passing arguments by value (like in C) or by refencence (like C++ -…
Nick
  • 10,309
  • 21
  • 97
  • 201
0
votes
1 answer

semantical reasons for pass-by-reference

I'm really wondering, is pass by reference (or passing a pointer) ever the "best" solution semantically wise? Especially for primitive types it feels like: int x = 5; x = foo(x); is more natural than int x = 5; foo(x); I first thought about code…
paul23
  • 8,799
  • 12
  • 66
  • 149
0
votes
5 answers

Is passing a variable declared to be a pointer to a function the same as passing the address of a variable not declared to be a pointer?

Even after years of C, pointers still confuse me. Are these two the same: int *num; someFunc(num) and int num; someFunc(&num); Declaring a variable with * makes it a pointer, & turns a variable (momentarily) into a pointer?
Marty
  • 5,926
  • 9
  • 53
  • 91
0
votes
1 answer

Copying an object javascript in order to pass by value

From a function, I would like to return a modified version of an object passed as an argument, without affecting the original object (i.e. side effect free). So essentially I want to pass by value. I have tried to do this by modifying and…
zenna
  • 9,006
  • 12
  • 73
  • 101
0
votes
3 answers

Pass by Value (Queue) in Java gives an unexpected output

I have a very small program: public static void main(String[] args) { Queue queue = new…
user793623
-1
votes
0 answers

value of a field in a struct is changing when the struct is copied to another variable(pass by value)

I defined a struct and have fields in them one of the fields is map[string]interface{} type. I've instantiated the struct globally, I've created a table struct and looping through it. In each loop I'm assigning the instantiated global struct to…
-1
votes
2 answers

Why did this python list appear to get passed "by value"?

I defined the following function: def myFunction(a): print(f'In function, initial a = {a}, {id(a)}, {type(a)}') a = a*10 print(f'In function, final a = {a}, {id(a)}, {type(a)}') return a When I try calling it like so: a_list_var =…
-1
votes
1 answer

Can I directly traverse this linked list using just the head pointer?

I've just started DSA recently and this question may seems trivial, but big thanks to those who take it seriously. This is my program: #include #include struct node{ int data; struct node *link; }; typedef struct node…