+ 1
What is the difference between passing by reference and passing the reference?
8 Respostas
+ 1
I'm not sure if both of those are things, maybe just some inconsistent typing?
+ 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/
+ 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 :)
+ 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?” :)
+ 1
i don't know if this is correct or not,i am assuming that she wanted to ask this.
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
}
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?
0
actually i was thinking it might be something like this..
https://www.geeksforgeeks.org/passing-by-pointer-vs-passing-by-reference-in-c/