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

Passing values in Python

When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
76
votes
7 answers

Passing an array by reference in C?

How can I pass an array of structs by reference in C? As an example: struct Coordinate { int X; int Y; }; SomeMethod(Coordinate *Coordinates[]){ //Do Something with the array } int main(){ Coordinate Coordinates[10]; …
Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112
75
votes
9 answers

In PHP (>= 5.0), is passing by reference faster?

In PHP, function parameters can be passed by reference by prepending an ampersand to the parameter in the function declaration, like so: function foo(&$bar) { // ... } Now, I am aware that this is not designed to improve performance, but to…
Hanno Fietz
  • 30,799
  • 47
  • 148
  • 234
74
votes
9 answers

Can you pass-by-reference in R?

Can you pass by reference with "R" ? for example, in the following code: setClass("MyClass", representation( name="character" )) instance1 <-new("MyClass",name="Hello1") instance2 <-new("MyClass",name="Hello2") array =…
Pierre
  • 34,472
  • 31
  • 113
  • 192
66
votes
8 answers

Are pointers considered a method of calling by reference in C?

In my University's C programming class, the professor and subsequent book written by her uses the term call or pass by reference when referring to pointers in C. An example of what is considered a 'call by reference function' by my professor: int…
Insane
  • 889
  • 10
  • 21
60
votes
9 answers

Passing arguments by reference

I want to ask if it is possible to pass arguments to a script function by reference: i.e. to do something that would look like this in C++: void boo(int &myint) { myint = 5; } int main() { int t = 4; printf("%d\n", t); // t->4 boo(t); …
RomanM
  • 6,363
  • 9
  • 33
  • 41
60
votes
4 answers

What's the difference between "var" and "out" parameters?

What's the difference between parameters declared with var and those declared with out? How does the compiler treat them differently (e.g., by generating different code, or by changing which diagnostics it issues)? Or do the different modifiers…
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
58
votes
1 answer

Does Swift have something like "ref" keyword that forces parameter to be passed by reference?

In Swift, structs and value types are passed by value by default, just like in C#. But C# also has a very usable ref keyword, that forces the parameter to be passed by reference, so that the same instance could be changed inside the function and…
Max Yankov
  • 12,551
  • 12
  • 67
  • 135
57
votes
5 answers

javascript pass object as reference

i have a object, which is getting passed in many different functions inside a function. these functions may or may not change the value of the object, but if they do change it, then i would like to get the latest changes on object. following is what…
Basit
  • 16,316
  • 31
  • 93
  • 154
56
votes
1 answer

Implementing standard software design patterns (focus on MVC) in R

Currently, I'm reading a lot about Software Engineering, Software Design, Design Patterns etc. Coming from a totally different background, that's all new fascinating stuff to me, so please bear with me in case I'm not using the correct technical…
Rappster
  • 12,762
  • 7
  • 71
  • 120
55
votes
6 answers

Java is NEVER pass-by-reference, right?...right?

Possible Duplicate: Is Java “pass-by-reference”? I found an unusual Java method today: private void addShortenedName(ArrayList voiceSetList, String vsName) { if (null == vsName) vsName = ""; else vsName =…
Troy Nichols
  • 1,234
  • 1
  • 13
  • 17
55
votes
7 answers

Pass a string by reference in Javascript

I want to create a string and pass it by reference such that I can change a single variable and have that propagate to any other object that references it. Take this example: function Report(a, b) { this.ShowMe = function() { alert(a + " of " +…
Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125
54
votes
4 answers

Why isn't this object being passed by reference when assigning something else to it?

I know that in JS, objects are passed by reference, for example: function test(obj) { obj.name = 'new name'; } var my_obj = { name: 'foo' }; test(my_obj); alert(my_obj.name); // new name But why doesn't the below work: function test(obj) { …
Dev555
  • 2,128
  • 4
  • 30
  • 40
53
votes
6 answers

Pass an array to a function by value

Below is a snippet from the book C Programming Just the FAQs. Isn't this wrong as Arrays can never be passed by value? VIII.6: How can you pass an array to a function by value? Answer: An array can be passed to a function by value by declaring in …
Zuzu
  • 3,363
  • 5
  • 27
  • 16
53
votes
13 answers

Passing optional parameter by reference in c++

I'm having a problem with optional function parameter in C++ What I'm trying to do is to write function with optional parameter which is passed by reference, so that I can use it in two ways (1) and (2), but on (2) I don't really care what is the…
Moomin
  • 1,846
  • 5
  • 29
  • 47