+ 1

What is the difference between passing by reference and passing the reference?

18th Apr 2018, 1:09 PM
Raunak Jalan
Raunak Jalan - avatar
8 ответов
+ 1
I'm not sure if both of those are things, maybe just some inconsistent typing?
18th Apr 2018, 1:47 PM
What was my name again?
What was my name again? - avatar
+ 1
Ugh, I believe you meant to say "What's the difference between passing by reference and passing by value?" And if so, then the passing my reference means your passing the variable itself so when a function makes a change it affects the original variable as well. Passing by value on the other hand means that a copy of the original variable is made so the original state isn't altered. I saw a post on Instagram today actually that gave a simple visual. Here (: https://www.instagram.com/p/BhtPG7HAUF2/
18th Apr 2018, 2:04 PM
Don
Don - avatar
+ 1
Mmh, maybe it’s a trick, because when you pass BY reference you give the function the address of the variable pointer (THE actual reference, indeed), so there’s no difference at all between BY and THE reference :)
18th Apr 2018, 2:38 PM
Luigi
Luigi - avatar
+ 1
Ok! So if this is what was meant to say your teacher, that’s it! But the question should be slightly different, something like “what’s the difference between passing the ponter and passing the reference?” :)
18th Apr 2018, 3:03 PM
Luigi
Luigi - avatar
+ 1
i don't know if this is correct or not,i am assuming that she wanted to ask this.
18th Apr 2018, 3:04 PM
Raunak Jalan
Raunak Jalan - avatar
0
When you're passing a variable by value you just pass a copy of its value. The original variable won't be changed. Once the below function is completed its life cycle, the copy of the a value is destroyed. Example: void changeValue(int a) { a = 7; } int main() { int a = 2; std::cout << a << std::endl; //Output: 2 changeValue(a); std::cout << a << std::endl; //Output: 2 } ___________________________________________ When passing by reference, instead, you basically pass a pointer to the variable memory address, and not a copy, so if you're going to make changes to the variable, it will be to the originary a variable. Example: void changeValue(int &a) // <-- & passes for ref { a = 7; } int main() { int a = 2; std::cout << a << std::endl; //Output: 2 changeValue(a); std::cout << a << std::endl; //Output: 7 }
18th Apr 2018, 2:24 PM
Luigi
Luigi - avatar
0
no Luigi Don What was my name again? it was asked by our teacher today in quiz what is difference between passing by reference and passing the reference?
18th Apr 2018, 2:30 PM
Raunak Jalan
Raunak Jalan - avatar
0
actually i was thinking it might be something like this.. https://www.geeksforgeeks.org/passing-by-pointer-vs-passing-by-reference-in-c/
18th Apr 2018, 2:43 PM
Raunak Jalan
Raunak Jalan - avatar