< Previous | Contents | Next >
After I declare and initialize myScore and yourScore, I send them to cout. As you’d expect, 150 and 1000 are displayed. Next I call badSwap(), which passes both
arguments by value. This means that when I call the function with the following line, copies of myScore and yourScore are sent to the parameters x and y.
badSwap(myScore, yourScore);
Specifically, x is assigned 150 and y is assigned 1000. As a result, nothing I do with x and y in badSwap() will have any effect on myScore and yourScore.
When badSwap() executes, x and y do exchange values—x becomes 1000 and y becomes 150. However, when the function ends, both x and y go out of scope. Control then returns to main(), in which myScore and yourScore haven’t changed. When I then send myScore and yourScore to cout, 150 and 1000 are displayed again. Sadly, I still have the tiny score and you still have the large one.