Python Pass by Value vs Pass by Reference (&)
By default in C++, parameters are passed by value (a clone copy is made in memory). Modifying a copy does not change the original variable! Adding an ampersand (&) passes by reference, creating an alias directly to the original memory box.
"Pass by Value is photocopying a document and scribbling notes on the copy—the original master document in the filing cabinet remains untouched. Pass by Reference (&) is handing someone the original document so any edits they make alter the master file directly."
Deep Dive: How It Works
Pass by Value (default): void doubleIt(int x) { x *= 2; }. Creates a separate copy; original variable in caller is unchanged.
Pass by Reference (&): void doubleIt(int &x) { x *= 2; }. x becomes a direct alias of the original variable; modifying x modifies the caller's variable!
Const Reference (const Type &): Passes large objects (like big strings or vectors) without making expensive memory copies, while preventing accidental modifications.
Syntax Blueprint
// Pass by Value (Copy)
void increment(int x) { x++; }
// Pass by Reference (Direct Alias)
void increment(int &x) { x++; }The & symbol attaches the parameter directly to the caller's variable.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Expecting pass-by-value functions to mutate variablesWhy it happens: Assuming function parameters automatically modify the original variable.
How to fix: Add the & symbol to the parameter type: void update(int &val).
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Double a value by reference
Write void doubleValue(int &n) { n *= 2; }
In main(), declare int score = 50;, call doubleValue(score);, and print "Score: 100".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.