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

What is the difference between pass by value and pass by reference in c++?

27th Mar 2017, 2:00 AM
SHRIDHAR JOSHI
15 Answers
+ 12
When passing a variable by value to a a function you are actually passing as argument a copy of the variable's value. When the function ends, the copy of the argument's value is removed from the stack (destroyed). Thus, any changes applied to such value won't affect the original variable. Passing by reference allows the programmer to pass to a funcition a variable's value itself rather than a copy of it. Passing by reference is used when it is desirable to modify the value of a varibale via a function.
27th Mar 2017, 2:18 AM
NNNicomedes
NNNicomedes - avatar
+ 7
In pass by value,You are passing the value to the function which is temporarily stores in stack.. In pass by reference,You are passing the memory location using pointer which permanently stores in stack but if we free up the memory by deleting the pointer's value..that memory can be freed up..but note that we cant delete pointer,as we can use it again..we can only free up its value assigned by it..
30th Mar 2017, 12:53 PM
Prahar pandya
Prahar pandya - avatar
+ 6
Passing by value - Copy of actual parameter is pass to formal parameter. - Values of actual parameter remain unchanged.- most of the functions fall under this category. void swap (int x, int y){ int temp; temp = x, x = y, y = temp; cout<<"\nValues During swapping (function call): \n"; cout<<"\tFirst Value = "<<x<<"\n\tSecond Value = "<<y; } int main (void){ int a = 10, b = 20; cout<<"Values before swapping: \n"; cout<<"\tFirst Value = "<<a<<"\n\tSecond Value = "<<b; swap(a,b); // x=a, y=b cout<<"\nValues after swapping:\n"; cout<<"\tFirst Value = "<<a<<"\n\tSecond Value = "<<b; } Output: Values before swapping are: First Value = 10 Second Value = 20 Values during swapping (function call): First Value = 20 Second Value = 10 Values after swapping: First Value = 10 Second Value = 20 Passing by references - Instead of sending values of actual parameters, their respective memory addresses are is passed to formal parameters. - Values of actual parameter may change. - This is used for modifying the values of actual parameters or returning more than one value out of the function. - This can be implemented by using addressof (&) operator before formal parameters. void swap (int &x, int &y){ int temp; temp = x, x = y, y = temp; cout<<"\nValues During swapping (function call): \n"; cout<<"\tFirst Value = "<<x<<"\n\tSecond Value = "<<y; } int main (void){ int a = 10, b = 20; cout<<"Values before swapping: \n"; cout<<"\tFirst Value = "<<a<<"\n\tSecond Value = "<<b; swap(a,b); // x=a, y=b cout<<"\nValues after swapping:\n"; cout<<"\tFirst Value = "<<a<<"\n\tSecond Value = "<<b; } Output: Values before swapping are: First Value = 10 Second Value = 20 Values during swapping (function call): First Value = 20 Second Value = 10 Values after swapping: First Value = 20 Second Value = 10
27th Mar 2017, 4:26 AM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
+ 1
One way to think of pass by value and pass by reference is like making a copy or using the original. In the case of pass by value, it's like you are making a copy of the variable just for use in the function. Whereas in the case of pass by reference it's almost like you are using the original in the function. Thus, any changes made to the "pass by value" copy do not affect the actual variable passed in. While the "pass by reference" instance can result in modification of that variable.
27th Mar 2017, 6:19 PM
Cristopher
Cristopher - avatar
+ 1
If your function don't change argument and arguments are big ( array - object- vector ... ) it's better to pass by reference . Because pass by reference don't make a copy. And you can write const to tell other this function don't change that. void test (const vector<int> & v)
27th Mar 2017, 6:23 PM
mohamad mahjoob
mohamad mahjoob - avatar
+ 1
it's just like cash on delivery and online payment.
29th Mar 2017, 5:09 PM
Rakshith Shetty
Rakshith Shetty - avatar
+ 1
Call by value: When we pass any variable to a function, any changes made to the variable will only be valid only in that function and not in the original value.Simply saying, chages will be reflected locally. Call by reference: When we pass any variable with reference to a function, any changes made to the variable will be reflected to the original variable. Simply saying, chages will be reflected globally.
1st Apr 2017, 3:40 AM
Rohtash Sethi
Rohtash Sethi - avatar
+ 1
its been a long time since I coded C++ but i think pass by value returns the value of the variabnle and pass by reference returns the slot indexed of the var
2nd Apr 2017, 12:48 PM
koder
koder  - avatar
0
If you change the value of an argument of a function in the function body, then depend on how you pass the argument, its value will be changed or not. pass by value: the value of the argument is not changed after the function call. pass by reference: the value of the argument will be changed after the function call. For example: void addOneValue(int n) { n++; } void addOneRef(int& n) { n++; } then: int x = 5; addOneValue(x); cout << x << endl; addOneRef(x); cout << x << endl; will give output: 5 6
27th Mar 2017, 2:15 AM
hdo
hdo - avatar
0
pass by value means to call function using variables declared and pass by reference means call function using memory allocated
27th Mar 2017, 8:33 PM
Jaspreet Singh
Jaspreet Singh - avatar
0
Suppose you have a document and you want to check it by others so if you copy the document and send for checking then the correction is made in the copy of the document not in original document it is something like Call by Value, and if you pass the original documents for checking then the correction is made in the original document and it is something like Call by Reference.
28th Mar 2017, 7:04 AM
Aditya Sharma
Aditya Sharma - avatar
0
eg : x=5; pass by value - directly passing the value ie; x pass by reference - passing the address of the value ie; &x if any change in value occur aftr passing the value it won't affect the original value. change in value occurs in pass by reference as u r actually referring the address of the variable.
31st Mar 2017, 12:56 PM
Sanal Kv
Sanal Kv - avatar
0
value and reference . . . . just kidding
1st Apr 2017, 3:32 PM
mani krishna
mani krishna - avatar
2nd Apr 2017, 5:47 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - 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:26 AM
Nikhil kumar