I don't know the difference between these codes when I use return-by-reference

i don't care lyrics
ed sheeran i don't care
i don't care i love it
i don't care song
i don't care live
ed sheeran songs 2019
i don't care chords
beautiful people

I'm learning object oriented programming. I don't know about the difference between these codes. Both of them use return-by-reference, but Code#1 is working well but Code#2 isn't working well. Professor said Code #2 has a problem when I store the return value of swap function, it causes the problem. But I don't know why.

Please tell me why and the difference of two codes.

Code #1:

#include <iostream>

using namespace std;

struct Pair {
    int first;
    int second;
};

Pair& swap(Pair&);

int main()
{
    Pair p1 = {10, 20};
    Pair p2 = swap(p1);
    return 0;
}

Pair& swap(Pair& pair)
{
    int temp;

    temp = pair.first;
    pair.first = pair.second;
    pair.second = temp;

    return pair;
}

Code #2:

#include <iostream>

using namespace std;

struct Pair {
    int first;
    int second;
};

Pair& swap(int num1, int num2);

int main()
{
    Pair p = swap(10, 20);
    return 0;
}

Pair& swap(int num1, int num2)
{
    int temp;

    temp = num1;
    num1 = num2;
    num2 = temp;

    Pair pair = {num1, num2};
    return pair;
}

The difference is that in Code #1, the main() function declares the variable pair, so it exists in the scope of main(). In Code #2, the swap() function declares the variable pair so it exists only in the scope of the swap() function and once you leave the swap function, the variable is destroyed.

Ed Sheeran - I Don't Care (Live At Abbey Road), I don't have a problem with Justin Bieber though, mumbles incoherently I just like the song way Duration: 5:30 Posted: Dec 6, 2019 Directed by Kelly Makin. With Denise Richards, Dean Cain, Karen Cliche, Olivia Jones. Denise Richards plays Lauren, a divorced wedding-planner who falls for the groom-to-be (Dean Cain).

At very first, you can avoid unnecessary code:

int temp = num1;
num1 = num2;
num2 = temp;

Pair pair = {num1, num2};

Why swapping numbers first, just create the pair with numbers swapped:

Pair pair = {num2, num1};
//              ^     ^

But now let's consider the differences (I dropped all parts irrelevant to the problem, i. e. the actual swapping):

Pair& swap(Pair& pair)
{
    return pair;
}

In the first variant, you get a pair by reference. The pair must be created outside and is passed into the function:

Pair p;  // created outside
swap(p); // swap uses reference to p outside
// p still exists
// swap returns just a reference to the same p it received
// -> you can use it for assignment:
Pair pp = swap(p);

Notice that your function swapped the original p it received by reference, so both p and pp contain the same content. So these two snippets of code are equivalent:

Pair p;
Pair pp = swap(p);

Pair p;
swap(p); // ignoring the return value
Pair pp = p;

In the second variant, you create the pair inside the function!

Pair& swap(int num1, int num2)
{
    Pair pair = {num1, num2};
    return pair;
}

But the pair's lifetime ends with the function exiting. So you return a reference to a pair that actually already has been destroyed, which yields undefined behaviour.

Exactly the same happens, if you accept the pair by value:

Pair& swap(Pair pair) // not a reference -> pair is copied into local variable
{
    return pair; // returning reference to local -> undefined behaviour!
}

In all cases you want to return local variables, you need to return them by value:

Pair swap(int, int)
{
    Pair pair;
    return pair; // return by value, value will be copied into target variable
                 // (actually, due to copy elision, directly written there)
}

Returning by value can be useful, too, if you don't want to modify the pair passed to the function (just for completeness, it's not that you need to change your function to). You'd then make sure, though, that you won't modify the pair passed to. You could accept by const reference and create the copy inside; most simple way, though, is accepting by value, which creates the copy directly when receiving the paramter:

Pair swap(Pair pair) // notice: both references dropped
{
    return pair;
};

Now, p and pp do differ (well, assuming you actually implemented the swapping, of course):

Pair p;
Pair pp = swap(p);

I Don't Care (Ed Sheeran and Justin Bieber song), We Don't Need to Cancel George Washington. But We Should Be Honest About Who He Was. We can only have a real discussion about  I Don't (The Cruz Brothers Book 1) - Kindle edition by Fox, Ella. Download it once and read it on your Kindle device, PC, phones or tablets. Use features like bookmarks, note taking and highlighting while reading I Don't (The Cruz Brothers Book 1).

in #code2 the Pair exist only in scope of swap function

in #code1 you need a temp becouse you change exist pair and you want to save the value before you change it. in #code2 (let's ignore about main problem) you need just form pair from the valus you got

    Pair& swap(int num1, int num2)
{
    Pair pair = {num1, num2};
    return pair;
}

and if you think about it you don't swap pair becouse you haven't pair..

We Don't Need to Cancel George Washington. But We Should Be , Why I Don't Like Cisco. Jun. 26, 2020 10:11 PM ET. |. 1 comment. |. | About: Cisco Systems, Inc. (CSCO), Includes: AAPL, CTXS, GOOG, IBM, INTC, JNPR, MSFT  I Do (But I Don't) was produced for the Lifetime cable network, where it first aired on September 13, 2004. A woman is torn between her heart and her business in this made-for-cable romantic comedy.

Why I Don't Like Cisco (NASDAQ:CSCO), “This is as bad as it gets, and yet the president will not confront the Russians on this score, denies being briefed,” Pelosi said on Sunday. “I don't  Co-Founder of I Do Now I Don’t, Josh Opperman launched the revolutionary consumer-to-consumer Marketplace for recycled diamonds, jewelry, and watches together with his sister, Mara, in 2007.

Pelosi on reported bounties on US troops: 'I don't know what the , Talk about “out of left field”. I don't know if he can handle the Pats locker room… but we'll see now. Hopefully the desire to stay in the league fuels  'Cause I don't care as long as you just hold me near You can take me anywhere And you're making me feel like I'm loved by somebody I can deal with the bad nights When I'm with my baby, yeah Ooh, ooh, ooh, ooh, oooh, ooh (No) [Justin Bieber:] I don't like nobody but you, it's like you're the only one here I don't like nobody but you, baby, I don

Cam Newton: I'm excited as I don't know what, What We Know — And What We Don't — About 'Asymptomatic COVID-19'. June 26, 2020. Dr. Paul Sax. Data from Partners HealthCare shows that about 1  Editors’ Notes Rapper Earl Sweatshirt’s third album is a dark, fascinating trip to the bottom of the self. Lyrically, Earl is a singular talent, capable of dense, expressive lines that flip back and forth between humor and pain, despair and resolve.

Comments
  • You may not return reference to function-local objects since those do not exist after function has returned.
  • code1: swap function takes input argument as Pair& and returns a Pair&. code 2: swap takes 2 integers input and returns Pair&. So, in first case, the input parameter Pair& is created before calling the swap function and then passed to the swap function as an argument which works correctly. In code 2, the swap function takes an argument and tries to return a Pair& which is created inside the swap function itself .code 2 swap function is trying to return something which is local to the swap function itself i.e. something which does not exist outside swap function so its error.
  • When a function returns, all its local variables become invalidated so your reference is in fact pointing to a variable whose value is undefined and your program runs on undefined behavior.
  • GCC warns about your example #2 that what you do does not make sense. coliru.stacked-crooked.com/a/2cc8404c9a258fc1