+ 3
What is the difference between pass by reference and pass by value?
3 ответов
+ 3
With pass by value you can not really modify the real value of the variable but you just create a copy that have new address.By reference you get and set the address of the variable then you can modify its value so no copy is created but the same variable passed using pointer to point to variable's address in the memory.
+ 3
HBhZ_C has a great explanation right here. It's also important to note how your particular language handles pass by value. For example, any primitive values should just be copied, but when you pass a non-primitive value, are you *moving* the value into the function and changing ownership or are you *copying* the value into the function.
A good example of this is Rust which uses move semantics by default meaning it moves the value into the function by default and you must .clone() a value to achieve passing a value by copy.
+ 1
Passing by refrence is usually faster than passing by value (specially in cases which you are passing a string or a vector or a 2D array to your function), because it takes time to create a copy of your data.