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
32
votes
9 answers

Use of the & operator in C++ function signatures

I'm currently reading through Accelerated C++ and I realized I don't really understand how & works in function signatures. int* ptr=# means that ptr now holds the address to num, but what does that mean? void DoSomething(string& str) from…
Ziv
  • 2,755
  • 5
  • 30
  • 42
31
votes
1 answer

:= (pass by reference) operator in the data.table package modifies another data table object simultaneously

While testing my code, I found out the following: If I assign a data.table DT1 to DT and change DT afterwards, DT1 changes with it. So DT and DT1 seem to be internally linked. Is this intended behavior? Although I'm not a programming expert, this…
Christoph_J
  • 6,804
  • 8
  • 44
  • 58
31
votes
4 answers

How to pass "literal" integers by reference in C++

To avoid the inefficiency of copy-by-value when calling a function (say, "fillRect"), I want to pass the parameters by reference. If I supply the parameters as declared local variables, it works fine. But if I supply any as "literal" integers, I get…
aaaidan
  • 7,093
  • 8
  • 66
  • 102
31
votes
8 answers

Is "pass by reference" bad design?

So, I just started learning Java, and I discovered that there's no such thing as "pass by reference". I'm porting an application from C# to Java, and the original application has ints and doubles that are either "ref" or "out" parameters. At first I…
vmayer
  • 985
  • 2
  • 9
  • 18
30
votes
3 answers

Is this undefined behaviour in C ? If not predict the output logically

Code 1 #include int f(int *a, int b) { b = b - 1; if(b == 0) return 1; else { *a = *a+1; return *a + f(a, b); } } int main() { int X = 5; printf("%d\n",f(&X, X)); } Consider this C code. The question here is to…
user5863049
30
votes
3 answers

ref and out in C++/CLI

I know that the C++/CLI code void foo(Bar^% x); transforms into Void foo(ref Bar x); What is the C++/CLI code that becomes Void foo(out Bar x); ?
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
30
votes
3 answers

Variable p passed by reference before being initialized

I have a Human class with a function that takes any amount of people and determines if someone is older than any of those people, then returns an array with the people he/she is older than. func isOlderThan(people: Human...) -> [Human] { var p:…
Brejuro
  • 3,421
  • 8
  • 34
  • 61
30
votes
6 answers

'pass parameter by reference' in Ruby?

In Ruby, is it possible to pass by reference a parameter with value-type semantics (e.g. a Fixnum)? I'm looking for something similar to C#'s 'ref' keyword. Example: def func(x) x += 1 end a = 5 func(a) #this should be something like func(ref…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
29
votes
7 answers

Java string value change in function

I have this very awkward question... void changeString(String str){ str = "Hello world": } main(){ String myStr = new String(""); changeString(myStr); } When main returns, the value is still "" and not "Hello world". Why is that? Also,…
Yura
  • 313
  • 1
  • 4
  • 6
29
votes
10 answers

How to Increment a class Integer references value in java from another method

package myintergertest; /** * * @author Engineering */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { //this one does not increment Integer n = new…
cchampion
  • 7,607
  • 11
  • 41
  • 51
29
votes
6 answers

What is useful about a reference-to-array parameter?

I recently found some code like this: typedef int TenInts[10]; void foo(TenInts &arr); What can you do in the body of foo() that is useful, that you could not do if the declaration was: void foo(int *arr); // or, void foo(int arr[]); //…
Dan
  • 5,929
  • 6
  • 42
  • 52
29
votes
4 answers

How to access original activity's views from spawned background service

I have an activity called A, and on the selection of menu item 0, it spawns service B, which starts a runnable C in a new thread. I have a TextView in activity A, which I want to access in thread C. I've tried making the TextView a public static…
montooner
  • 1,112
  • 4
  • 17
  • 29
28
votes
6 answers

Can I change String object's value passed to my method?

I found the following question Is Java "pass-by-reference" or "pass-by-value"?. I read almost all of it, but could not find out yet what should I do if I want the foo(-) method, to change my String's value? (maybe or not reference too, it doesn't…
merveotesi
  • 2,145
  • 15
  • 53
  • 84
28
votes
6 answers

How do I pass large numpy arrays between python subprocesses without saving to disk?

Is there a good way to pass a large chunk of data between two python subprocesses without using the disk? Here's a cartoon example of what I'm hoping to accomplish: import sys, subprocess, numpy cmdString = """ import sys, numpy done = False while…
Andrew
  • 2,842
  • 5
  • 31
  • 49
28
votes
11 answers

How can you extend Java to introduce passing by reference?

Java is pass-by-value. How could you modify the language to introduce passing by reference (or some equivalent behavior)? Take for example something like public static void main(String[] args) { String variable = "'previous String reference'"; …