+ 1

What is the point of passing by ref?

Why not just make the method return a value?

7th Jan 2018, 8:37 PM
Joshua Farr
Joshua Farr - avatar
4 Respuestas
+ 9
consider situation that you change multiple variables in function, you can return just one variable from function, but can send multiple variables to function by reference
7th Jan 2018, 8:51 PM
Vukan
Vukan - avatar
+ 9
If your input to a function/method is a big object (an array with 10000 entries for example) and you just want to modify a few elements, there is no need to create a copy of the whole object. But that's precisely what happens, if it's passed by value. Passing by reference allows you to modify the object itself without needing to copy it.
7th Jan 2018, 11:18 PM
Tob
Tob - avatar
7th Jan 2018, 8:53 PM
David Akhihiero
David Akhihiero - avatar
+ 5
Copies use more resources, including CPU time, stack space and memory. It's the difference between referring to a person by name and stopping to scan and make a complete clone of them every time they come up. Are there use cases in each scenario? (depends on what you need)
7th Jan 2018, 11:36 PM
Kirk Schafer
Kirk Schafer - avatar