C++ pass by VALUE vs pass by REFERENCE? 📧

Поділитися
Вставка
  • Опубліковано 6 жов 2024
  • #pass #value #reference
    Pass by value vs pass by reference tutorial example explained

КОМЕНТАРІ • 19

  • @BroCodez
    @BroCodez  2 роки тому +13

    #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
      @farisvaljevac1478 Рік тому +1

      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

  • @Clockwerk777
    @Clockwerk777 Рік тому +7

    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?

  • @ganapathipasupathi1666
    @ganapathipasupathi1666 5 місяців тому +1

    many thanks sir, my many confusions are cleared. I have been watching this series from one week before, Sir did the 👏🙌good job

  • @lastwaveundergroundsaviour7037
    @lastwaveundergroundsaviour7037 2 роки тому +3

    i have been trying to do this, now i realize after watching this video that i was doing it backwards. ahaha thanks man

  • @adnanaman4391
    @adnanaman4391 Місяць тому

    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

  • @FrederikWollert
    @FrederikWollert 5 місяців тому

    Nice Video. But should also say that pass-by-value/reference means the same as call-by-value/reference.

  • @shervin9561
    @shervin9561 9 місяців тому +1

    Thanks a lot!

  • @kristijanlazarev
    @kristijanlazarev 7 місяців тому +1

    I see mine are swapping even without passing by reference, why

  • @user-dy1vf7lu3i
    @user-dy1vf7lu3i Рік тому +1

    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?

    • @aliceylan1211
      @aliceylan1211 Рік тому +5

      You could but the point of the video is to demonstrate the difference between passing by reference and passing by value

    • @ishaqmangansakan7574
      @ishaqmangansakan7574 Рік тому

      There is a string function, but it just happens to prefer to use a void function.

  • @renusoni6077
    @renusoni6077 4 місяці тому

    Is it a good practice to always pass variables by reference to the functions?

  • @garyalvarado2155
    @garyalvarado2155 2 місяці тому

    if this is the case shouldnt we always pass parameters by reference?

  • @lastwaveundergroundsaviour7037
    @lastwaveundergroundsaviour7037 2 роки тому

    oooo interesting

  • @gamsterilyass2952
    @gamsterilyass2952 3 місяці тому

    bro

  • @user-lyf4isnt7daijobu
    @user-lyf4isnt7daijobu 3 місяці тому

    #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

  • @Jm2010g5
    @Jm2010g5 11 днів тому

    You're the best, man
    Resusbcribing = in progress