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

Python Lambda Mutability

class TestClass(object): def __init__(self): self.value = 100 self.x = lambda: self.value.__add__(100) self.run() def run(self): self.x() print self.value t = TestClass() #Output: 100 I…
sheepez
  • 986
  • 1
  • 10
  • 26
2
votes
8 answers

C++ pass by ref compilation error

With C++, I struggle to understand one compilation error. I have this function, with this given signature: void MethodNMMS::tryNMSA(double factor, double temperature,double& funcWorst,int& iWorst, double& funcTry, double* funcEvals) { …
kiriloff
  • 25,609
  • 37
  • 148
  • 229
2
votes
2 answers

Pass by null reference in PHP

In PHP I need to pass some arguments to a function by reference. I don't want to write 2 different methods for similar behaviour. So i need to select behaviour by argument. But I can't pass null by reference. So I created a dummy array. So i run it…
trante
  • 33,518
  • 47
  • 192
  • 272
2
votes
2 answers

different by ref question using a simple =

I have a variant on the by ref question. I know all about calling with the ref or our parameters and how it affects variables and their values. I had this issue with a DataTable and I want to know why a datatable is different than a simple integer…
Jaydel Gluckie
  • 328
  • 4
  • 12
2
votes
4 answers

Why Java not allow C# "out" feature for method parameters

Possible Duplicate: Why there is no way to pass by reference in java Can anybody tell me why exactly Java does not provide C# "out" type feature when dealing with method parameters to pass by reference ? I mean why would not it allow us to pass…
Faisal Mq
  • 5,036
  • 4
  • 35
  • 39
2
votes
3 answers

PHP: Pass by reference

Passing by reference: output is: -test - but when I do the output is: -test- Why can't I pass this by…
dukevin
  • 22,384
  • 36
  • 82
  • 111
2
votes
4 answers

When Pass by reference becomes mandatory over pass by address/pointer

Can someone please give some examples where pass by reference is preferred over pass by pointer. Basically both offer the same functionality but I want to know where pass by reference becomes mandatory. Also, please suggest how to…
Kundan Kumar
  • 1,974
  • 7
  • 32
  • 54
2
votes
1 answer

C++ passage by pointer and modification of values

until there I trusted that method like bool solverMethod::buildSimplex(double** simplex_ , double* funcEvals_, double* initPt_) { // things } would change values for simplex, funcEvals_, initPt_ in the method where it is called (passage by…
kiriloff
  • 25,609
  • 37
  • 148
  • 229
1
vote
5 answers

What is the size of ( variable = &anotherVar )

What is the size of ( variable = &anotherVar ) int y = 10; // the size of y is 4 bytes int & x = y; // what is the size of x that receives the address of y
faressoft
  • 19,053
  • 44
  • 104
  • 146
1
vote
1 answer

Pass by reference behavior from ncurses

I've recently started playing around with ncurses. As far as I understand, C doesn't support passing by reference. However, there are some functions that seem to exhibit such a behavior: calling getmaxyx(stdscr, maxRow, maxCol); assigns the the…
math4tots
  • 8,540
  • 14
  • 58
  • 95
1
vote
3 answers

How do I populate a struct pointer with global struct reference inside a C function?

I am new to C and having trouble understanding why my_struct_ptr (main) is nil in the following example. How would I assign the address of a struct in the my_structs array to the my_struct_ptr pointer within the get_my_struct_by_name…
TenaciousC
  • 13
  • 4
1
vote
2 answers

Why does perl "hash of lists" do this?

I have a hash of lists that is not getting populated. I checked that the block at the end that adds to the hash is in fact being called on input. It should either add a singleton list if the key doesn't exist, or else push to the back of the list…
Overflown
  • 1,830
  • 2
  • 19
  • 25
1
vote
4 answers

manipulating multidimensional arrays with functions in C++

I am trying to modify the contents of a 2D array in C++ using a function. I haven't been able to find information on how to pass a 2D array to a function by reference and then manipulate individual cells. The problem I am trying to solve has the…
1
vote
1 answer

JNA: Pointer to a pointer to a struct

I have a function call: long foo(mystruct **list) where the definition of mystruct is typedef struct { otherstruct *bar[64]; } mystruct; and I'm trying to get at the (JNA) Structure[] corresponding to bar. My current function definition is…
Andy Shulman
  • 1,895
  • 3
  • 23
  • 32
1
vote
3 answers

c++ classes can only be passed by reference

Is there a way I can make my custom class be passed by reference only? Basically, I have the following class: class A{ private: int _x; public: A(int y){ _x = y; } }; Can I make it to where I can ONLY pass it by reference? And it will…
poy
  • 10,063
  • 9
  • 49
  • 74