LessonThis is a lesson restated in new words. Any figure in it is an example from the source, not this desk's record.
A swap written against its parameters exchanges those parameters only. If the parameters are copies, the caller’s numbers stay put. The lecture starts with 1 and 0, calls a by-value swap, and still prints 1 and 0. Pass the addresses with pointers, or pass C++ references, and the same three assignments print 0 and 1.
Why it matters
Call-stack copies are the default in C and C++ for ordinary arguments. The function receives its own doubles, works on them, and those doubles are destroyed when the function returns. That is safe and it is also why a textbook swap silently does nothing to the variables you cared about. Any routine that must change a caller’s object, fill in a result, or point at a large structure without duplicating it has to say so in the parameter list. The swap is the smallest place to see the difference, because the correct printout is obvious: the two numbers should trade.
Pointers are one way to say so. A C++ reference is the other way in this lecture, with less visual noise at the call. Both are pass-by-reference in the sense that the function works on the caller’s objects. The by-value version works on temporaries.
The idea in plain words
The function takes two doubles, stores the first in a temporary, assigns the second into the first, and assigns the temporary into the second. In main, the doubles start at 1 and 0. The call passes those names. The print that follows shows 1 and then 0. The body ran. It ran on copies. When the copies died at the closing brace, the assignments died with them. The caller’s 1 and 0 were never the variables inside the function.
To aim at the caller’s slots, the parameters become pointers to doubles. The call passes addresses. Inside the function the asterisks follow those addresses, so the temporary reads the caller’s first number, the first slot receives the caller’s second number, and the second slot receives the temporary. After the return, the print is 0 and then 1. The temporary is still a local double. The difference is that the assignments through asterisks wrote the originals. This is the same shared-slot idea as a pointer demo: an address is not a second number, and writing through it changes the first number.
The C++ spelling drops the asterisks in the body. Declare the parameters as double references, with an ampersand on the parameter, and write the swap as if the parameters were ordinary doubles. At the call, pass the variables directly. The ampersand in the declaration means “this name is an alias for the caller’s object,” so the plain assignments are already assignments to the caller. The lecture’s run of that version also prints 0 and 1. Two syntaxes, one effect. The reference version is presented as the less confusing of the two once the pointer version has made the memory story clear. Dereferencing a pointer at every use is where people lose the plot. A reference hides that syntax after the declaration.
How to use it
Write the by-value swap first and require the failure. If 1 and 0 come back swapped, you are not looking at the lecture’s program. Something else is already passing an address. The failed print is the control experiment.
What usually goes wrong
Mixing the styles, such as reference parameters but asterisks in the body, or pointer parameters without asterisks, either will not compile or will swap the addresses’ numeric values in a way that does not match either demo. Pick one style per function.
Forgetting the ampersand at the call site of the pointer version passes a double where an address is required. That is a compile error in the usual setup, and it is a better failure than a silent 1 and 0. The silent failure is the by-value signature that type-checks perfectly and does the wrong job.
Questions people ask
Why did the first swap print 1 and 0?
The parameters were copies on the stack. The exchanges happened to the copies, and the copies were destroyed on return. The caller’s doubles were never assigned.
What do the pointer version and the reference version print?
Both print 0 and 1 in the lecture, starting from 1 and 0. Pointers do it with explicit asterisks. References do it with an ampersand on the parameters and ordinary assignments in the body.
When would you still pass by value?
When the function should not change the caller, and a copy is acceptable. The lecture’s first signature is that case, and the unchanged print is the proof that the caller was insulated.