What is the difference between pass by reference and pass by value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is the difference between pass by reference and pass by value?

25th Oct 2020, 9:10 PM
Khaled ^^ خالد القريشي‎
Khaled ^^ خالد القريشي‎ - avatar
3 Answers
+ 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.
25th Oct 2020, 9:33 PM
HBhZ_C
HBhZ_C - avatar
+ 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.
25th Oct 2020, 9:51 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 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.
26th Oct 2020, 4:47 AM
Soheil
Soheil - avatar