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
-2
votes
3 answers

Why can't I re-initialize an Array in a function?

In C#, arrays are passed to functions by reference, which means that this code : static void FillArray(int[] A) { for (int i = 0; i < A.Length; i++) { A[i] = -1; } } static void Main(string[] args) { int[] A = new int[10]; …
Ayman El Temsahi
  • 2,600
  • 2
  • 17
  • 27
-2
votes
2 answers

C: Variable is uninitialized for linked list

I'm relatively new to C and I was creating a program that involves a linked-list. Here is a very abbreviated version of the code that's giving me trouble. #include #include #include #define STRLEN 100 struct Gene {…
matnnar
  • 3
  • 2
-2
votes
1 answer

The object does not change after being passed through a constructor. Why?

Hi I have the following code: public class Dog { private String name; private int size; public Dog(String name, int size){ this.name = name; this.size = size; } public void changeSize(int newSize){ size =…
Haris Ghauri
  • 547
  • 2
  • 7
  • 27
-2
votes
1 answer

c# changes value of one object property from other

I'm creating one object by using values from other object.Like that: MAP.cs int[][] map = {......}; Mob m = new Mob(0,0,map); And calling Mob's class function Move() m.Move(); Move function looks like this: int xp = (int)Math.Floor(x / 32); int yp…
Berrigan
  • 438
  • 4
  • 23
-2
votes
3 answers

Java - changing a variable through pass-by-value

So I've read about the pass-by-value nature of Java and I've tried to change my variable after passing it to a function by having the function return the variable again. I didn't succeed in that. My code public class Logic { private int…
KSHMR
  • 741
  • 1
  • 9
  • 24
-2
votes
1 answer

Why Does code.runnable.com Allow Me to Change a Variable's Value in Java?

I heard that Java integers are pass by value, so why does the following code work in code.runnable.com? public class HelloWorld { public static void main(String[] args) { int number = 0; number = 2; System.out.println(number); …
-2
votes
1 answer

How can i add a return statement to magicTime?

I'm trying to find the output of the following code. How can i add a return statement to magicTime? The out put should be a: 10 b: 30 c: a: 10 b: 30 #include using namespace std; int magicTime(int a, int &b, const int &c){ …
-2
votes
2 answers

Passing References by Value in Java

Dog.setName("Kutta"); // this is how you call a setter method String Naam = Dog.getName(); System.println(Naam); //Kutta Naam = "Kutte ki Ma"; System.println(Dog.getName()); // Is this *Kutte ki Ma* or is it *Kutta*? And Why? If Java passes…
hrishi1990
  • 325
  • 3
  • 12
-2
votes
1 answer

pass by value and pass by reference for x and y

Given the pseudo code below, what will be the output of the program, when the two parameters x and y are passed by value, and when they are passed by reference? int x = 1; int y = 1; int count = 0; while count < 3{ addnumbers(x, y); println…
robert
  • 23
  • 1
  • 5
-2
votes
4 answers

Passing obejct to function and assigning null , won't change the object in Main()

Consider this code : using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Foo { public int x { get; set; } public String y…
JAN
  • 21,236
  • 66
  • 181
  • 318
-2
votes
3 answers

Passing STL containers by value, does the calling code see the updates?

If I had code like: void x(std::set s){ s.insert(6.0); } int main(){ std::set s; s.insert(5.0); x(s); //Would 6 be seen here? //Or would it only be seen if I had void x(std::set& s)? } and does the…
user997112
  • 29,025
  • 43
  • 182
  • 361
-2
votes
5 answers

HashMap not modifiying object contents

I have a hashmap in which i am putting a reference to an object. DateTest test100 = new DateTest("ABC",100); dateTestMap.put(96, test100); I am passing this reference variable to a method where i am assigning it a different object and…
Adithya
  • 2,923
  • 5
  • 33
  • 47
-2
votes
3 answers

Why Java is purely Pass by value?

Why does java doesn't support pass by reference? Is there any specific reason for that?*
-3
votes
1 answer

how can i handle Java pass-by-value? i need pass-by-reference

I have such piece of code: DataTableFactory TempDataTableFactory = new DataTableFactory(); DataTable tempDataTable = TempDataTableFactory.getInstance(); tempDataTable = dataTable; ExecutedArguments e = new…
brtb
  • 2,201
  • 6
  • 34
  • 53
-3
votes
2 answers

C++ Pass-by-value, shouldn't the memory_addresses of the two variables be copied and binded into different places?

I'm practicing C++, and facing the Pass-by-value/reference topic. What I learned from BroCode is that, when we call a swap function by the parameters' value, both of their memory_addresses will be different from the original ones. #include…
ian
  • 55
  • 4