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
12
votes
5 answers

Good practice to edit objects "by reference"?

Let's say I've got a type called Superstar. Now I want to have a method that does some work and edits some properties of a Superstar object. Here are two ways of how I could implement this. Way 1 would be the following: private Superstar…
user321068
12
votes
2 answers

PowerShell passing two arrays by reference to a function

At the moment I am trying to pass in two array items by reference to a function I have written which loads data into those arrays. However, once this function loses scope the arrays appear as blank. If I use the ref keyword to pass them into the…
Matthew Pigram
  • 1,400
  • 3
  • 25
  • 65
12
votes
4 answers

Java Pass By Value and Pass By Reference

i have done the following sample with to check my knowledge import java.util.Map; public class HashMap { public static Map useDifferentMap(Map valueMap) { valueMap.put("lastName", "yyyy"); return…
Joe
  • 4,460
  • 19
  • 60
  • 106
12
votes
4 answers

Unable to pass an address of array of type char *[2] to function taking char ***

My XCode (default compiler should be clang?) shows me on this code a warning: Incompatible pointer types passing 'char *(*)[2]' to parameter of type 'char ***' (when calling func) void func (char ***arrayref, register size_t size) { ///…
bwoebi
  • 23,637
  • 5
  • 58
  • 79
12
votes
1 answer

Mockito: what if argument passed to mock is modified?

We came across really nasty problem with Mockito. Code: public class Baz{ private Foo foo; private List list; public Baz(Foo foo){ this.foo = foo; } public void invokeBar(){ list = Arrays.asList(1,2,3); …
Piotr Sobczyk
  • 6,443
  • 7
  • 47
  • 70
11
votes
5 answers

Can I reduce memory allocation by passing DateTime parameter by reference in c#?

In C#, is there any significant reduction in memory allocation when passing a DateTime reference as a parameter to a function as opposed to passing it by value? int GetDayNumber(ref DateTime date) vs int GetDayNumber(DateTime date) The code…
11
votes
2 answers

c++ function: pass non const argument to const reference parameter

suppose I have a function which accept const reference argument pass, int func(const int &i) { /* */ } int main() { int j = 1; func(j); // pass non const argument to const reference j=2; // reassign j } this code works fine.according to…
fuyi
  • 2,573
  • 4
  • 23
  • 46
11
votes
1 answer

Are arguments passed to methods by reference or value?

Can anyone expand upon, correct, or verify what I feel is happening when you pass arguments to a method in Ruby. Are any of these points wrong? Am I missing any pieces? Everything in Ruby is an object. Variables are references to objects (When…
slindsey3000
  • 4,053
  • 5
  • 36
  • 56
11
votes
3 answers

Is there any practical reason why std::get_if (std::variant) takes a variant argument by pointer instead of by value/&/const&?

I've never used std::get_if, and since its name is different from std::get, I don't see a reason why its argument should be a pointer¹ (whereas std::get has a by-reference parameter). ¹If it was named std::get too, then overload resolution would be…
Enlico
  • 23,259
  • 6
  • 48
  • 102
11
votes
4 answers

Using ostream as a reference (C++)

I have a homework assignment where the header file is provided to us, and is unchangeable. Im having trouble figuring out how to correctly use a "display" function, so here is the relevant code. The header file: #ifndef SET_ #define SET_ typedef…
user212562
  • 195
  • 1
  • 2
  • 6
11
votes
1 answer

Swift for in - get entries as reference

Is there a way to make the for .. in loop return references to the entries of a collection instead of copies? Say I have an array points of CGPoint objects and I want to loop over them and pass each point to a function adjustPoint that can modify…
Keiwan
  • 8,031
  • 5
  • 36
  • 49
11
votes
10 answers

Meaning of pass by reference in C and C++?

I am confused about the meaning of "pass by reference" in C and C++. In C, there are no references. So I guess pass by reference means passing a pointer. But then why not call it pass by pointer? In C++, we have both pointers and references (and…
Registered User
  • 2,239
  • 3
  • 32
  • 58
11
votes
5 answers

PHP - Recursively set each array element's key to the value of a child element when given the childs key name

I'll start by showing a non-recursive example Non- recursive example $given_key_name = 'site_id'; $rows[] = array( 'site_id' => '0', 'language_id' => '1', 'name' => 'sitename', 'description' =>'site desc', ); $results =…
TarranJones
  • 4,084
  • 2
  • 38
  • 55
11
votes
7 answers

Scheme pass-by-reference

How can I pass a variable by reference in scheme? An example of the functionality I want: (define foo (lambda (&x) (set! x 5))) (define y 2) (foo y) (display y) ;outputs: 5 Also, is there a way to return by reference?
Cam
  • 14,930
  • 16
  • 77
  • 128
11
votes
3 answers

Pass array by reference and modify values C++

I want to write a function which takes inArray[3] = {1,2,3,4} and an outArray[3], and modifies outArray[3] within the function to now contain values = {3,4,1,2}. int main{ int inArray[4] = {1,2,3,4}; int outArray[4]; myFunction(&inArray,…
Rohit
  • 174
  • 1
  • 1
  • 8