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

Returning object from function

I am really confused now on how and which method to use to return object from a function. I want some feedback on the solutions for the given requirements. Scenario A: The returned object is to be stored in a variable which need not be modified…
brainydexter
  • 19,826
  • 28
  • 77
  • 115
18
votes
4 answers

C++ pass list as a parameter to a function

I'm trying to build a very simple address book. I created a Contact class and the address book is a simple list. I'm trying to build a function to allow the user to add contacts to the address book. If I take my code outside of the function, it…
Adrian
  • 609
  • 2
  • 11
  • 22
18
votes
8 answers

What is the difference when I write Func1(int &a) and Func1(int *a)?

Possible Duplicate: Difference between pointer variable and reference variable in C++ As I am starting with C++, I found the operation below confusing. I got to know about passing by reference and passing by value. But recently I came across…
Simsons
  • 12,295
  • 42
  • 153
  • 269
18
votes
7 answers

Return code or out parameter?

I'm making a method to fetch a list of filenames from a server but I have come to a problem that I cannot answer. The method returns two things: an SftpResult which is an enum with a variety of return codes. the list of filenames. Of these three…
Nobody
  • 4,731
  • 7
  • 36
  • 65
18
votes
5 answers

can a method parameter pass an object by reference but be read-only?

C#: can you make it so that a method parameter passes an object by reference but is read-only? eg: void MyMethod(int x, int y, read-only MyObject obj) where obj is an object reference but this object cannot be modified during the method. Can this…
CJ7
  • 22,579
  • 65
  • 193
  • 321
18
votes
6 answers

Scope in javascript acting weird

Object are passed with their reference in javascript. Meaning change in that object from any where should be reflected. In this case, the expected output was {} for console.log(a) function change(a,b) { a.x = 'added'; a = b;//assigning a as…
Suman Lama
  • 946
  • 1
  • 9
  • 26
18
votes
3 answers

Invalid initialization of non-const reference of type

In the following code, I'm not able to pass a temporary object as argument to the printAge function: struct Person { int age; Person(int _age): age(_age) {} }; void printAge(Person &person) { cout << "Age: " << person.age << endl; } int…
jeffreyveon
  • 13,400
  • 18
  • 79
  • 129
18
votes
2 answers

PHP: Only variables can be passed by reference

I am getting this error on line 57: $password = str_replace($key, $value, $password, 1); As far as I can tell, I am only passing in variables. Here is some more context: $replace_count = 0; foreach($replacables as $key => $value) { …
Edward Yu
  • 776
  • 2
  • 6
  • 15
17
votes
2 answers

Testing optional arguments in PHP

I have a few "setter" methods across classes, and for convenience I've added an optional parameter $previous, which takes an argument by reference and populates it with the existing value before replacing it with the new one. For example: public…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
17
votes
4 answers

Does Array.find method return a copy or a reference of the matched element form given array?

What does Array.find method returns value some specifical copy of a found value or the reference from the array. I mean what it returns value or reference of the matched element from the given array.
MD Jahid Hasan
  • 786
  • 1
  • 9
  • 19
17
votes
3 answers

How does the C# garbage collector find objects whose only reference is an interior pointer?

In C#, ref and out params are, as far as I know, passed by passing only the raw address of the relevant value. That address may be an interior pointer to an element in an array or a field within an object. If a garbage collection occurs, it's…
munificent
  • 11,946
  • 2
  • 38
  • 55
17
votes
5 answers

java (beginner) : returning an object ? is it returned as a constant reference or what?

I have a function that returns a user-defined object. First I want to know if that object is returned by reference and what if it was private? Also, how do I return it as Constant (final) reference because I don't want someone to mess with it? I'm…
Ismail Marmoush
  • 13,140
  • 25
  • 80
  • 114
17
votes
13 answers

Consequences of only using stack in C++

Lets say I know a guy who is new to C++. He does not pass around pointers (rightly so) but he refuses to pass by reference. He uses pass by value always. Reason being that he feels that "passing objects by reference is a sign of a broken…
BefittingTheorem
  • 10,459
  • 15
  • 69
  • 96
17
votes
1 answer

How to return an inout (reference) in a swift function?

I'd like a function to return a reference of an array: var a = [1, 2] var b = [3, 4] func arrayToPick(i:Int) -> [Int] { return i == 0 ? a : b } inout var d = arrayToPick(0) d[0] = 6 println(a[0]) // 1 println(d[0]) // 6 I'm unable to return…
ldiqual
  • 15,015
  • 6
  • 52
  • 90
17
votes
4 answers

c++ treat the address location as an integer

I want to know if there is anyway that I can store the address location of a variable as an integer value. For example, let's say that I have a number stored in some location in memory int i= 20; and we know that for example, the location of the…
Rudy01
  • 1,027
  • 5
  • 18
  • 32