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
15
votes
10 answers

What is the difference between passing by reference in Java and passing a pointer in C?

I have been studying Java for a few months and am now starting to learn C. I am a little confused, I was under the impression that passing an object by reference and passing a pointer to that object were the same thing: I thought the difference was…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
15
votes
3 answers

Why c# don't let to pass a using variable to a function as ref or out

Possible Duplicate: Passing an IDisposable object by reference causes an error? Why doesn't C# allow passing a variable from a using block to a function as ref or out? This is my code: using (Form s = new Form()) { doSomthing(ref s); } The…
Saleh
  • 2,982
  • 5
  • 34
  • 59
15
votes
6 answers

Const References to Pointers

I came across some code that used a method like this: QList itemList; void addItem(Item* const& item) { itemList.append(item); } Now, I can't see any meaningful difference between that and this: QList itemList; void addItem(Item*…
cgmb
  • 4,284
  • 3
  • 33
  • 60
15
votes
2 answers

Why doesn't java support pass by reference like C++

I have read every where that primitive datatype and object references are passed by value? I have tried searching in Google why doesn't java support pass by reference , but I only get java does not support pass by reference and I couldn't find any…
Searock
  • 6,278
  • 11
  • 62
  • 98
15
votes
2 answers

A property, indexer or dynamic member access may not be passed as an out or ref parameter

Hello I'm having trouble figuring this out. I have these structs and classes. struct Circle { ... } class Painting { List circles; public List circles { get { return circles; } } } I am trying to…
CantMutate
  • 153
  • 1
  • 1
  • 4
15
votes
4 answers

Is pass-by-value/reference equivalent to making a deep/shallow copy, respectively?

To reword the question in case someone types it into the search bar differently: Is pass-by-value the same as making a deep copy, and is pass-by-reference the same as making a shallow copy? If not, what is the difference? In Python, the language I'm…
15
votes
2 answers

Storing const reference to an object in class

This sounds like a basic question, but I didn't find any comprehensive answer, so here it is. Consider this code snippet: struct A { const std::string& s; A(const std::string& s) : s(s) {} }; int main() { A a("abc"); std::cout <<…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
15
votes
5 answers

PHP: check if object/array is a reference

Sorry to ask, its late and I can't figure a way to do it... anyone can help? $users = array( array( "name" => "John", "age" => "20" ), array( "name" => "Betty", "age" => "22" ) ); $room = array( …
lepe
  • 24,677
  • 9
  • 99
  • 108
15
votes
3 answers

Should I pass a lambda by const reference.

Typically I use the following pattern when accepting a lambda as an argument to a function (A template class passed-by-value): template void higherOrderFunction(Function f) { f(); } Does this copy (the closure of) the…
jwheels
  • 517
  • 1
  • 7
  • 23
15
votes
1 answer

Python and Java parameter passing

I've seen in several places, including the Python documentation that Python uses pass by "assignment" semantics. Coming from a Java background, where the common mistake of saying "Java passes primitives by value, and objects by reference" is as a…
orrymr
  • 2,264
  • 4
  • 21
  • 29
15
votes
3 answers

Is passing by reference then copying and passing by value functionally different?

Is there a functional difference between: void foo(const Bar& bar) { Bar bar_copy(bar); // Do stuff with bar_copy } and void foo(Bar bar) { // Do stuff with bar }
wrhall
  • 1,288
  • 1
  • 12
  • 26
15
votes
6 answers

PHP by-reference parameters and default null

Let's say we have a method signature like public static function explodeDn($dn, array &$keys = null, array &$vals = null, $caseFold = self::ATTR_CASEFOLD_NONE) we can easily call the method by omitting all parameters after…
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
15
votes
3 answers

What is the true meaning of pass-by-reference in modern languages like Dart?

Working with Futures in Dart, I've come across an interesting issue. import 'dart:async'; class Egg { String style; Egg(this.style); } Future cookEggs(List list) => new Future(() => ['omelette','over easy'].forEach((_) =>…
user1748315
15
votes
3 answers

C# parameters by reference and .net garbage collection

I have been trying to figure out the intricacies of the .NET garbage collection system and I have a question related to C# reference parameters. If I understand correctly, variables defined in a method are stored on the stack and are not affected by…
Yarko
  • 151
  • 1
  • 3
15
votes
5 answers

c++ * vs & in function declaration

Possible Duplicate: Difference between pointer variable and reference variable in C++ When should I declare my variables as pointers vs objects passed-by-reference? They compile to the same thing in assembly (at least run-time asymptotically) so…
Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213