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

python numpy and memory efficiency (pass by reference vs. value)

I've recently been using python more and more in place of c/c++ because of it cuts my coding time by a factor of a few. At the same time, when I'm processing large amounts of data, the speed at which my python programs run starts to become a lot…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
17
votes
9 answers

Passing char array by reference

I am passing a char array by reference but when I return from function and print the array, it displays nothing. What am I doing wrong? #include using namespace std; void func(char []); int main() { char a[100]; func(a); cout…
Naruto
  • 1,710
  • 7
  • 28
  • 39
16
votes
3 answers

Out parameters in C

void swap(int &first, int &second){ int temp = first; first = second; second = temp; } int a=3,b=2; swap(a,b); The compiler complaints that void swap(int &first, int &second) has a syntax error. Why? Doesn't C support references?
user1128265
  • 2,891
  • 10
  • 29
  • 34
16
votes
5 answers

modify variable within R function

How do I modify an argument being passed to a function in R? In C++ this would be pass by reference. g=4 abc <- function(x) {x<-5} abc(g) I would like g to be set to 5.
Alex
  • 19,533
  • 37
  • 126
  • 195
16
votes
8 answers

What exactly does "pass by reference" mean?

And who has the authority to decide? Edit: Apparently I haven't succeeded in formulating my question well. I am not asking how Java's argument passing works. I know that what looks like a variable holding an object is actually a variable holding a…
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
16
votes
1 answer

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new(), and pass by…
Andrew Moffat
  • 405
  • 3
  • 11
16
votes
8 answers

Is returning a whole array from a Perl subroutine inefficient?

I often have a subroutine in Perl that fills an array with some information. Since I'm also used to hacking in C++, I find myself often do it like this in Perl, using references: my @array; getInfo(\@array); sub getInfo { my ($arrayRef) = @_; …
Frank
  • 64,140
  • 93
  • 237
  • 324
16
votes
3 answers

Array seems to be getting passed by reference in Java, how is this possible?

I can post more code if I need to, but before that I would like to just ask a general question about the following method in which an array is passed, and then set to another array, but for some reason the original array, the one being passed in, is…
jbernie2
  • 173
  • 1
  • 1
  • 6
16
votes
5 answers

C++ 64 bit int: pass by reference or pass by value

This is an efficiency question about 64 bit ints. Assuming I don't need to modify the value of a "int" parameter, should I pass it by value or reference. Assuming 32 bit machine: 1) 32 bit int: I guess the answer is "pass by value" as "pass by…
JP19
16
votes
7 answers

Modify elements of vector (by value, by reference) Function C++

I have a function where I have to modifiy the values of a vector. is it a good practice in C++ to return the vector? Function 1: vector RemoveSpecialCharacters(vector words) { for (vector::iterator it=words.begin();…
Hani Goc
  • 2,371
  • 5
  • 45
  • 89
16
votes
3 answers

C# Inconsistent results using params keyword

Given the following method: static void ChangeArray(params string[] array) { for (int i = 0; i < array.Length; i++) array[i] = array[i] + "s"; } This works if I call it passing a array of strings: string[] array = {"Michael",…
Bruno Santos
  • 724
  • 7
  • 19
16
votes
1 answer

Node.js V8 pass by reference

I wonder how memory is managed in V8. Take a look at this example: function requestHandler(req, res){ functionCall(req, res); secondFunctionCall(req, res); thirdFunctionCall(req, res); fourthFunctionCall(req, res); }; var http =…
onlineracoon
  • 2,932
  • 5
  • 47
  • 66
15
votes
3 answers

Passing array arguments by reference

I came across a function with this signature. void foo(char (&x)[5]) { } This is the syntax for passing a fixed size char array by reference. The fact that it requires parentheses around &x strikes me as unusual. It is probably part of the C++03…
Eddy Pronk
  • 6,527
  • 5
  • 33
  • 57
15
votes
1 answer

Is it legal to pass va_list as a reference to a function in C++?

Upon code review/clang-tidy runs, I came across a function with a signature like this: void appendFoo(const char * fmt, va_list& rVaList); I have never seen this before. Afaik, you can pass va_list by value (and maybe by pointer?). This brings me…
user3284229
  • 151
  • 3
15
votes
8 answers

how to pass unlimited arguments by reference in php

I have php function that has an unlimited number of args which I am getting from func_get_args(). I have some operations with arguments (changing string or doing something) and I want this to be like a passing argument by reference. is it…
bzzb
  • 151
  • 1
  • 3