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
13
votes
4 answers

candidate function not viable: expects an l-value for 3rd argument

Calculate nth power of P (both p and n are positive integer) using a recursive function myPowerFunction(int p, int n, int ¤tCallNumber). currentCallNumber is a reference parameter and stores the number of function calls made so far.…
Rajat Patel
  • 139
  • 1
  • 1
  • 4
13
votes
3 answers

Why is calling a function (such as strlen, count etc) on a referenced value so slow?

I've just found something very strange in PHP. If I pass in a variable to a function by reference, and then call a function on it, it's incredibly slow. If you loop over the inner function call and the variable is large it can be many orders of…
John Carter
  • 53,924
  • 26
  • 111
  • 144
13
votes
2 answers

VBA Pass Array By Reference and Modify Contents

VBA question. I'm using it to make Solidworks macros, but that's not important. Can someone explain the syntax to pass an array (1-Dimensional of type Double, with length of three) to a subroutine or function so I can modify the contents. I know…
Geoffrey
  • 143
  • 1
  • 1
  • 4
13
votes
2 answers

How can arguments to variadic functions be passed by reference in PHP?

Assuming it's possible, how would one pass arguments by reference to a variadic function without generating a warning in PHP? We can no longer use the '&' operator in a function call, otherwise I'd accept that (even though it would be error prone,…
outis
  • 75,655
  • 22
  • 151
  • 221
13
votes
2 answers

Is there a rational explanation for this PHP call-by-value behavior? Or PHP bug?

PHP 5.5.12. Consider this: aq [1] => bq [2] => cq ) Now consider:
Nairebis
  • 283
  • 1
  • 8
13
votes
3 answers

Passing a vector by reference to function, but change doesn't persist

I'm new to C++ so please forgive the noob question. In my program, I pass a vector of "Employee" objects to a function by reference, which calls an Employee member function on each object in the vector, to raise "salary" by "r" percent (in this case…
Reid
  • 1,109
  • 1
  • 12
  • 27
13
votes
9 answers

Origin of term "reference" as in "pass-by-reference"

Java/C# language lawyers like to say that their language passes references by value. This would mean that a "reference" is an object-pointer which is copied when calling a function. Meanwhile, in C++ (and also in a more dynamic form in Perl and PHP)…
Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
13
votes
4 answers

C++ Passing `this` into method by reference

I have a class constructor that expects a reference to another class object to be passed in as an argument. I understand that references are preferable to pointers when no pointer arithmetic will be performed or when a null value will not…
Dai
  • 141,631
  • 28
  • 261
  • 374
12
votes
3 answers

Delphi passing parameters by reference or by value/copy

Context 1 var text:String; text:='hello'; myFunc(text); Context2 function myFunc(mytext:String); var textcopy:String; begin textcopy:=mytext; end; myFunc on the Context2 was called from the Context1, the local variable mytext is pointing…
Vitim.us
  • 20,746
  • 15
  • 92
  • 109
12
votes
8 answers

How can I simulate pass by reference in Java?

I'm a complete Java noob. I know that Java treats all parameters as pass by value and there are several other threads where people explain this. For example, in C++ I can do: void makeAThree(int &n) { n = 3; } int main() { int myInt = 4; …
Casey Patton
  • 4,021
  • 9
  • 41
  • 54
12
votes
5 answers

Why doesn't this work if in Ruby everything is an Object?

Considering that in the Ruby programming language everything is said to be an Object, I safely assumed that passing arguments to methods are done by reference. However this little example below puzzles me: $string = "String" def changer(s) s =…
jlstr
  • 2,986
  • 6
  • 43
  • 60
12
votes
1 answer

Function in R, passing a dataframe and a column name

Possible Duplicate: Pass a data.frame column name to a function I am trying to create a function in R where between the inputs there is dataframe and a column name. The code would be something like this: DT_CAP_COLUMN <-…
jpsfer
  • 594
  • 3
  • 7
  • 18
12
votes
2 answers

C# 7 ref return for reference types

I'm going through some code that uses the new features of C# 7 and uses the ref locals & returns feature. It seems pretty straight forward for value-types where the ref local variable gets a reference (to the actual storage), and updating that…
Arghya C
  • 9,805
  • 2
  • 47
  • 66
12
votes
4 answers

How to use references in Java?

I want to use reference in Java but I don't know how! for example in C++ we write: void sum(int& x) { ... } but in Java & sign is a compiler error! please help me to understand references in Java.
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
12
votes
7 answers

Passing to a Reference Argument by Value

Consider this simple program: vector foo = {0, 42, 0, 42, 0, 42}; replace(begin(foo), end(foo), foo.front(), 13); for(const auto& i : foo) cout << i << '\t'; When I wrote it I expected to get: 13 42 13 42 13 42 But instead I got: 13…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288