You could justa declare ans assign values of x and y. Then use std::swap(x,y) to swap their values. There is a function in C++ already there to swap 2 values. And at then end displayed the swapped values of x and y. #include int main(){ std::string x = "Bro"; std::string y = "Code"; std::swap(x, y); std::cout
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?
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
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?
#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
You could justa declare ans assign values of x and y. Then use std::swap(x,y) to swap their values. There is a function in C++ already there to swap 2 values. And at then end displayed the swapped values of x and y.
#include
int main(){
std::string x = "Bro";
std::string y = "Code";
std::swap(x, y);
std::cout
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?
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
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
Nice Video. But should also say that pass-by-value/reference means the same as call-by-value/reference.
Thanks a lot!
I see mine are swapping even without passing by reference, why
could you post the code here
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.
Is it a good practice to always pass variables by reference to the functions?
if this is the case shouldnt we always pass parameters by reference?
oooo interesting
bro
#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
You're the best, man
Resusbcribing = in progress