Questions tagged [pass-by-reference]

Pass by reference is an argument marshalling strategy whereby a variable's location in memory is passed to a function, rather than a copy of the variable's value, although the function appears in the source code to receive the variable itself rather than a pointer to it.

Passing by reference means that the memory address of a variable is passed rather than a copy of the variable's value.

This typically means that the function can modify the passed variable, assigning a new value to it. However for performance reasons, passing by reference may be useful even if the passed structure is not modified, as with the Pascal var modifier, and some programming languages have constructs (like the C const modifier) to disallow modification of a variable passed by reference.

4485 questions
24
votes
8 answers

Java Object Assignment

I am new to Java and I have some questions in mind regarding object assignment. For instance, Test t1 = new Test(); Test t2 = t1; t1.i=1; Assuming variable i is defined inside Test class, am I right to assume both t1 and t2 point to the same object…
user1238193
24
votes
11 answers

Confused, whether java uses call by value or call by reference when an object reference is passed?

public class program1{ public static void main(String args[]){ java.util.Vector vc=new java.util.Vector(); vc.add("111"); vc.add("222"); functioncall(vc); vc.add("333"); …
user617597
  • 788
  • 3
  • 9
  • 21
23
votes
3 answers

PHP: "... variables can be passed by reference" in str_replace()?

I created a function to print a prepared-statement-sql-string with the variables in it, based on what I found in this other StackOverflow question. Here is my code: foreach($params as $idx => $param) { if ($idx == 0) continue; $sql =…
Dexter
  • 3,072
  • 5
  • 31
  • 32
23
votes
3 answers

Java: How to pass byte[] by reference?

You can do it in .NET by using the keyword "ref". Is there any way to do so in Java?
Mahendra
  • 383
  • 1
  • 4
  • 10
23
votes
9 answers

C# pass by value vs. pass by reference

Consider the following code (I have purposefully written MyPoint to be a reference type for this example) public class MyPoint { public int x; public int y; } It is universally acknowledged (in C# at least) that when you pass by reference,…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
23
votes
6 answers

Object copied to second property via reference persists even after original property deleted?

I thought objects are passed as reference. But when I delete b, it still exists in c. Please see this example: This first part makes sense to me as its passed by reference: var a = {b: {val:true}}; a.c = a.b; a.b.val =…
Noitidart
  • 35,443
  • 37
  • 154
  • 323
23
votes
3 answers

Does forEach() bind by reference?

var arr = ['Foo']; arr.forEach(function(item){ console.log(item); item = 'Lorem'; console.dir(arr[0]); }); for (var item in arr){ arr[item] = 'Ipsum'; console.dir(arr[0]); } Like the code above shows, I noticed that changing the value…
Wottensprels
  • 3,307
  • 2
  • 29
  • 38
23
votes
3 answers

Whats the best way to send QStrings in a function call?

I would like to know what is the most efficient and practical way of sending a Qstring as a parameter to a function, in QT more specifically. I want to use a reference. The problem is I also want to instantiate that string in the function itself…
yan bellavance
  • 4,710
  • 20
  • 62
  • 93
23
votes
4 answers

How to pass a variable by value to an anonymous javascript function?

The Objective I want to dynamically assign event handlers to some divs on pages throughout a site. My Method Im using jQuery to bind anonymous functions as handlers for selected div events. The Problem The code iterates an array of div names and…
rism
  • 11,932
  • 16
  • 76
  • 116
22
votes
3 answers
22
votes
3 answers

How are arrays passed?

Are arrays passed by default by ref or value? Thanks.
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
21
votes
5 answers

Arguments by reference in Objective-C

I'm trying to pass an NSString by reference but it doesn't work. This is the function: +(void)fileName:(NSString *) file { file = @"folder_b"; } and this is the call: NSString *file; [function fileName:file]; nslog(@"%@",file); // and…
alex
  • 869
  • 4
  • 13
  • 28
20
votes
4 answers

Are swift classes inside struct passed by copy during assignment?

If I have a struct in swift with inside a class attribute and I copy the struct object, is the class attribute copied or passed by reference?
Nisba
  • 3,210
  • 2
  • 27
  • 46
20
votes
6 answers

Should small simple structs be passed by const reference?

I have always been taught that non-primitive types should be passed by const reference rather than by value where possible, ie: void foo(std::string str);//bad void foo(const std::string &str);//good But I was thinking today that maybe actually…
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
19
votes
5 answers

dict.get() method returns a pointer

Let's say I have this code: my_dict = {} default_value = {'surname': '', 'age': 0} # get info about john, or a default dict item = my_dict.get('john', default_value) # edit the data item[surname] = 'smith' item[age] = 68 my_dict['john'] =…
Armandas
  • 2,276
  • 1
  • 22
  • 27