What is pass by reference and pass by value and when we use them and why | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is pass by reference and pass by value and when we use them and why

12th Sep 2017, 8:05 PM
shveta patel
shveta patel - avatar
7 Answers
+ 3
When a parameter is passed by reference, the caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller's variable. When a parameter is passed by value, the caller and callee have two independent variables with the same value. If the callee modifies the parameter variable, the effect is not visible to the caller. However, if the value in question is a mutable reference-type object or otherwise indirectly references other values, then you can emulate call-by-reference in a call-by-value environment: if the callee modifies the object (or other values pointed to by the object), those modifications are visible to the caller. But the emulation isn't exactly the same, since only modifications to the object, not to the variable, are visible. This leads to contorted explanations like "call by value where the value is a reference". This somewhat confusing state of affairs is how many popular programming languages work today, and therefore people often confuse passing mutable objects by value with call-by-reference. https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value
12th Sep 2017, 8:15 PM
Akbar Khalilzadeh
Akbar Khalilzadeh - avatar
+ 2
https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value LoL...
12th Sep 2017, 8:16 PM
Mikhael Anthony
Mikhael Anthony - avatar
0
if u r a class 11th or 12th student then just read book Sumita Arora's Chapter Function(11th class) or Revision of class 11(12th class). :-)
13th Sep 2017, 1:44 PM
HOHO
0
I'm BSc honours 1st yr student and i haven't read cpp before guide me
13th Sep 2017, 1:54 PM
shveta patel
shveta patel - avatar
0
Umm I briefly tried to explain and clarify the use & difference of pass by value, pass by reference via reference & pointers, pass by reference in arrays via reference & pointers, & getting a pointer return type from function. https://code.sololearn.com/cNk82nOx24hw/?ref=app https://code.sololearn.com/c4dvux5CvxAA/?ref=app
9th Sep 2019, 11:30 AM
Nikhil kumar
- 2
pass by value, means create a copy of arguments and then pass it, so actual variable not changed, but in cases when u wanna change the value in actual variable , u pass it as reference, means you pass the address of the variable using '&' so that function will operate directly on value
12th Sep 2017, 8:23 PM
Morpheus
Morpheus - avatar
- 2
u ll get it better by code , wait I ll write a simple code that would forever solve your doubt
12th Sep 2017, 8:24 PM
Morpheus
Morpheus - avatar