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
126
votes
8 answers

How to do the equivalent of pass by reference for primitives in Java

This Java code: public class XYZ { public static void main(){ int toyNumber = 5; XYZ temp = new XYZ(); temp.play(toyNumber); System.out.println("Toy number in main " + toyNumber); } void…
Student
  • 4,481
  • 8
  • 27
  • 32
119
votes
6 answers

How do I pass the value (not the reference) of a JS variable to a function?

Here is a simplified version of something I'm trying to run: for (var i = 0; i < results.length; i++) { marker = results[i]; google.maps.event.addListener(marker, 'click', function() { change_selection(i); }); } but I'm…
ryan
  • 2,311
  • 3
  • 22
  • 28
116
votes
17 answers

C++ - passing references to std::shared_ptr or boost::shared_ptr

If I have a function that needs to work with a shared_ptr, wouldn't it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)? What are the possible bad side effects? I envision two possible cases: 1) inside the…
abigagli
  • 2,769
  • 4
  • 29
  • 32
108
votes
4 answers

Passing object by reference to std::thread in C++11

Why can't you pass an object by reference when creating a std::thread ? For example the following snippit gives a compile error: #include #include using namespace std; static void SimpleThread(int& a) // compile error //static…
austinmarton
  • 2,278
  • 3
  • 20
  • 23
108
votes
12 answers

Python functions call by reference

In some languages you can pass a parameter by reference or value by using a special reserved word like ref or val. When you pass a parameter to a Python function it never alters the value of the parameter on leaving the function.The only way to do…
Timothy Lawman
  • 2,194
  • 6
  • 24
  • 33
106
votes
5 answers

Difference between function arguments declared with & and * in C++

I typed the following example: #include double f(double* x, double* y) { std::cout << "val x: " << *x << "\n"; std::cout << "val y: " << *y << "\n"; return *x * *y; } double f2(double &x, double &y) { std::cout << "val x:…
tim
  • 9,896
  • 20
  • 81
  • 137
97
votes
7 answers

Which is more efficient: Return a value vs. Pass by reference?

I am currently studying how to write efficient C++ code, and on the matter of function calls, a question comes to mind. Comparing this pseudocode function: not-void function-name () { do-something return value; } int main () { ... …
thegreatjedi
  • 2,788
  • 4
  • 28
  • 49
97
votes
7 answers

C++ pass an array by reference

is this allowed to pass an array by reference ? void foo(double& *bar) Seems that my compiler says no. Why? What is the proper way to pass an array by reference? Or a work around? I have an array argument that my method should modify and that I…
kiriloff
  • 25,609
  • 37
  • 148
  • 229
95
votes
2 answers

How can I pass a reference to a function, with parameters?

Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application) I need to able to pass a reference to a function with a given set of parameters. Here is an example of passing a reference without…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
91
votes
7 answers

Java : Best way to pass int by reference

I have a parsing function that parses an encoded length from a byte buffer, it returns the parsed length as an int, and takes an index into the buffer as an integer arg. I want the function to update the index according to what it's parsed, i.e.…
fred basset
  • 9,774
  • 28
  • 88
  • 138
85
votes
11 answers

How can I pass an Integer class correctly by reference?

I am hoping that someone can clarify what is happening here for me. I dug around in the integer class for a bit but because integer is overriding the + operator I could not figure out what was going wrong. My problem is with this line: Integer i =…
sixtyfootersdude
  • 25,859
  • 43
  • 145
  • 213
85
votes
5 answers

Does C++ pass objects by value or reference?

A simple question for which I couldn't find the answer here. What I understand is that while passing an argument to a function during call, e.g. void myFunction(type myVariable) { } void main() { myFunction(myVariable); } For simple datatypes…
user3041058
  • 1,520
  • 2
  • 12
  • 16
85
votes
2 answers

What exactly is copy-on-modify semantics in R, and where is the canonical source?

Every once in a while I come across the notion that R has copy-on-modify semantics, for example in Hadley's devtools wiki. Most R objects have copy-on-modify semantics, so modifying a function argument does not change the original value I can…
Andrie
  • 176,377
  • 47
  • 447
  • 496
81
votes
8 answers

C++ view types: pass by const& or by value?

This came up in a code review discussion recently, but without a satisfactory conclusion. The types in question are analogues to the C++ string_view TS. They are simple non-owning wrappers around a pointer and a length, decorated with some custom…
acm
  • 12,183
  • 5
  • 39
  • 68
81
votes
4 answers

Are channels passed by reference implicitly

The go tour has this example for channels: https://tour.golang.org/concurrency/2 package main import "fmt" func sum(a []int, c chan int) { sum := 0 for _, v := range a { sum += v } c <- sum // send sum to c } func main()…
lhk
  • 27,458
  • 30
  • 122
  • 201