@@farisvaljevac1478 Yeah this function exists but he is trying to teach about reference and eventually how the built-in swap(x,y) function works under the hood to the beginners. :)
So if i understand this correctly, is this the same concept of variable scope? where basically when you pass by value you get local versions of X and Y, which is what the function swaps, rather than the main()value?
1:58 Why don’t we just return X and Y? Void function don’t have return types so we change the “void” to a “string” right? Or is there no such thing as a string function?
We are passing Memory Addresses from the main function to the made up Swap func through param &x&y , swapping activity done inside the swap function....then it goes on to display changed vals
so the ampersand & before a variable is the address of that variable in memory. I saw it used in my pointers lesson and this reference lesson. (i dont understand why the instructor seperated them)
No the ampersand is not a address but is a symbol make the parameter a reference parameter to the memory location variable of the argument. Hope you understand🙏🏾
#include void _swap(std::string x, std::string y); // passing by value // does not change value in place due to varying scopes(different memory addresses) void swap(std::string &x, std::string &y); // passing by reference // "reference" to memory address so that it changes in place int main() { std::string x = "Kool-Aid"; std::string y = "Water"; _swap(x,y); std::cout
#include
void swap(std::string &x, std::string &y);
int main()
{
std::string x = "Kool-Aid";
std::string y = "Water";
swap(x, y);
std::cout
@@farisvaljevac1478 Yeah this function exists but he is trying to teach about reference and eventually how the built-in swap(x,y) function works under the hood to the beginners. :)
many thanks sir, my many confusions are cleared. I have been watching this series from one week before, Sir did the 👏🙌good job
i have been trying to do this, now i realize after watching this video that i was doing it backwards. ahaha thanks man
So if i understand this correctly, is this the same concept of variable scope? where basically when you pass by value you get local versions of X and Y, which is what the function swaps, rather than the main()value?
RUclips is full of garbage, But your content bro, is gold. I subscribed you. Best of luck!
Is it a good practice to always pass variables by reference to the functions?
1:58
Why don’t we just return X and Y? Void function don’t have return types so we change the “void” to a “string” right? Or is there no such thing as a string function?
You could but the point of the video is to demonstrate the difference between passing by reference and passing by value
There is a string function, but it just happens to prefer to use a void function.
you can also only return 1 value as far as i know
@@KrizMo122 yeah, but if you want to return both, you can use vector of string(but then it will require change in other code too)
Nice Video. But should also say that pass-by-value/reference means the same as call-by-value/reference.
We are passing Memory Addresses from the main function to the made up Swap func through param &x&y , swapping activity done inside the swap function....then it goes on to display changed vals
so the ampersand & before a variable is the address of that variable in memory. I saw it used in my pointers lesson and this reference lesson. (i dont understand why the instructor seperated them)
No the ampersand is not a address but is a symbol make the parameter a reference parameter to the memory location variable of the argument.
Hope you understand🙏🏾
Did not get the pass by reference, wasnt that the same as by value in the example?
if this is the case shouldnt we always pass parameters by reference?
I see mine are swapping even without passing by reference, why
could you post the code here
Thanks a lot!
oooo interesting
#include
void _swap(std::string x, std::string y); // passing by value
// does not change value in place due to varying scopes(different memory addresses)
void swap(std::string &x, std::string &y); // passing by reference
// "reference" to memory address so that it changes in place
int main()
{
std::string x = "Kool-Aid";
std::string y = "Water";
_swap(x,y);
std::cout
why i success just after the first set of function , which the bro says wrong, lol
bro
You're the best, man
Resusbcribing = in progress