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
52
votes
7 answers

What's wrong with passing C++ iterator by reference?

I've written a few functions with a prototype like this: template int parse_integer(input_iterator &begin, input_iterator end); The idea is that the caller would provide a range of characters, and the function would…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
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
52
votes
3 answers

In C++/CLI, how do I declare and call a function with an 'out' parameter?

I have a function which parses one string into two strings. In C# I would declare it like this: void ParseQuery(string toParse, out string search, out string sort) { ... } and I'd call it like this: string searchOutput,…
Simon
  • 25,468
  • 44
  • 152
  • 266
51
votes
8 answers

How do I pass a primitive data type by reference?

How can I pass a primitive type by reference in java? For instance, how do I make an int passed to a method modifiable?
Neuquino
  • 11,580
  • 20
  • 62
  • 76
50
votes
2 answers

Go: invalid operation - type *map[key]value does not support indexing

I'm trying to write a function that modifies original map that is passed by pointer but Go does not allow it. Let's say I have a big map and don't want to copy it back and forth. The code that uses passing by value is working and is doing what I…
Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52
50
votes
1 answer

Call-time pass-by-reference has been removed

Possible Duplicate: Call-time pass-by-reference has been deprecated While it may be documented somewhere on the internet, I cannot find a solution to my problem. Since the PHP 5.4 update, pass-by-references have been removed. Now I have a…
Sam Smith
  • 617
  • 1
  • 5
  • 6
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…
46
votes
6 answers

Passing by reference to a constructor

I decided to see if assigning a reference to a member would make a member a reference. I wrote the following snippet to test it. There's a simple class Wrapper with an std::string as a member variable. I take take a const string& in the constructor…
rcplusplus
  • 2,767
  • 5
  • 29
  • 43
46
votes
3 answers

c# - should I use "ref" to pass a collection (e.g. List) by reference to a method?

Should I use "ref" to pass a list variable by reference to a method? Is the answer that "ref" is not needed (as the list would be a reference variable), however for ease in readability put the "ref" in?
Greg
  • 34,042
  • 79
  • 253
  • 454
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…
45
votes
1 answer

Correct usage of the Eigen::Ref<> class

Eigen has introduced the Ref<> class to write functions with Eigen objects as parameters without the use unnecessary temporaries, when writing template functions is not wanted. One can read about this here. When searching the internet further, I…
Nikolaus Ammann
  • 507
  • 1
  • 4
  • 12
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
42
votes
5 answers

How to pass callback in Flutter

Code will explain all: class ResultOverlay extends StatefulWidget { final bool _isCorrect; VoidCallback _onTap; ResultOverlay(this._isCorrect, this._onTap); ...... ...... } Its state class: class…
pallav bohara
  • 6,199
  • 6
  • 24
  • 45
42
votes
2 answers

How to make a copy of an object without reference?

It is well documented that PHP5 OOP objects are passed by reference by default. If this is by default, it seems to me there is a no-default way to copy with no reference, how?? function refObj($object){ foreach($object as &$o){ $o =…
acm
  • 6,541
  • 3
  • 39
  • 44