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

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

КОМЕНТАРІ • 24

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

    #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

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

      @@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. :)

  • @ganapathipasupathi1666
    @ganapathipasupathi1666 7 місяців тому +4

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

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

    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?

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

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

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

    UA-cam is full of garbage, But your content bro, is gold. I subscribed you. Best of luck!

  • @renusoni6077
    @renusoni6077 6 місяців тому +3

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

  • @FrederikWollert
    @FrederikWollert 8 місяців тому +2

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

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

      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.

    • @KrizMo122
      @KrizMo122 14 днів тому

      you can also only return 1 value as far as i know

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

    Thanks a lot!

  • @raiss4679
    @raiss4679 14 годин тому

    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)

  • @Stisse12
    @Stisse12 День тому

    Did not get the pass by reference, wasnt that the same as by value in the example?

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

    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

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

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

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

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

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

    oooo interesting

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

    #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

  • @WangBingzheng
    @WangBingzheng 17 днів тому

    why i success just after the first set of function , which the bro says wrong, lol

  • @gamsterilyass2952
    @gamsterilyass2952 6 місяців тому

    bro

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

    You're the best, man
    Resusbcribing = in progress