01 / 20

Pass-by-Value vs.
Pass-by-Reference

Understand how C++ passes arguments to functions: copies vs. aliases, and when to use each approach.

C++

What is a Function Argument?

When you call a function, you provide arguments: the actual data values you pass in. The function receives those arguments through its parameters: the variable names declared in the function signature.

void greet(string name); // "name" is a parameter greet("Alice"); // "Alice" is an argument

Pass-by-Value: Making a Copy

When a function parameter is declared without &, C++ creates an independent copy of the argument. Changes inside the function do not affect the original variable.

CALLER (main) x 10 stays 10 ✓ Unchanged COPY FUNCTION (addFive) num (copy) 10 num += 5 num 15

Pass-by-Reference: Sharing the Original

When a parameter is declared with &, the function receives a reference: an alias for the original variable. Changes inside the function directly modify the caller's data.

CALLER (main) x 10 becomes 15 Modified! ALIAS FUNCTION (addFive) num& (alias) points to x num += 5 x (via num) 15

Why Does This Matter?

Safety
Pass-by-value protects your original data from unexpected changes inside functions.
Efficiency
Pass-by-reference avoids copying large objects like vectors and strings, saving memory and time.
Side Effects
Knowing the difference prevents bugs where data changes unexpectedly, or fails to change when it should.

The Syntax Rule

// Pass-by-VALUE: no ampersand void funcA(int n); // Pass-by-REFERENCE: add & void funcB(int& n); // CONST reference: read-only alias void funcC(const int& n);
1
No &: the parameter is a fresh copy. The original cannot be changed.
2
With &: the parameter is an alias. Modifications propagate back to the caller.
3
With const &: read-only alias. Efficient but safe: no copies, no modifications.

Value vs. Reference: Side by Side

Pass-by-Value void add(int n) { n += 5; } int x = 10; add(x); x after call: 10 Copy modified, original safe ✓ No side effect Pass-by-Reference void add(int& n) { n += 5; } int x = 10; add(x); x after call: 15 Original directly modified Has side effect

How to Decide Which to Use

1
Does the function need to modify the original? Yes: use & (pass-by-reference). No: continue to step 2.
2
Is the argument large? (strings, vectors, objects) Yes: use const & to avoid expensive copy. No: continue to step 3.
3
Is it a small built-in type? (int, double, char, bool) Use pass-by-value: copying is cheap and safe.
4
Does the function return multiple results? Use reference parameters for the "extra" outputs.

Example 1: Pass-by-Value

void doubleIt(int n) { n = n * 2; cout << "Inside: " << n << endl; } int main() { int x = 7; doubleIt(x); cout << "After: " << x << endl; }
Inside function
Prints: Inside: 14
The local copy n becomes 14.
After call
Prints: After: 7
The original x is untouched.

Example 2: Pass-by-Reference

void doubleIt(int& n) { n = n * 2; cout << "Inside: " << n << endl; } int main() { int x = 7; doubleIt(x); cout << "After: " << x << endl; }
Inside function
Prints: Inside: 14
n is an alias for x.
After call
Prints: After: 14
The original x was modified through the alias.

Example 3: Classic Swap Function

// This WORKS: uses references void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int main() { int x = 3, y = 8; swap(x, y); // x is now 8, y is now 3 }
BEFORE x 3 y 8 swap(&) AFTER x 8 y 3 Without &, swap would only swap local copies and x, y would remain unchanged!

The const & Pattern

For large objects you want to read but not modify, use const &. This avoids the cost of copying while preventing accidental changes.

// BAD: copies the entire vector (slow) void print(vector<int> v) { ... } // GOOD: no copy, no modification void print(const vector<int>& v) { ... } // ERROR: can't modify a const reference void print(const vector<int>& v) { v.push_back(42); // Compiler error! }

Returning Multiple Values

C++ functions can only return one value. Use reference parameters when a function needs to produce multiple outputs.

void divmod(int a, int b, int& quotient, int& remainder) { quotient = a / b; remainder = a % b; } int main() { int q, r; divmod(17, 5, q, r); // q = 3, r = 2 }

Common Mistakes

✕ Forgetting the &
Writing a swap function like void swap(int a, int b). Without the & on the parameters, the function only swaps local copies.
✕ Passing a Literal by Reference
Calling func(5) when func takes int&. Literals have no memory address to reference. Use const & instead.
✕ Modifying a const Reference
Trying to change a const int& parameter. The compiler will reject this, but it confuses beginners.

Comparison Summary

Pass-by-Value Pass-by-Reference const &
Syntax int n int& n const int& n
Copy made? Yes No No
Can modify original? No Yes No
Accepts literals? Yes No Yes
Best for Small types Modify / multi-return Large read-only

Question 1 of 5

What is printed by this code?

void increment(int n) { n++; } int main() { int x = 5; increment(x); cout << x; }

Question 2 of 5

Which parameter declaration allows a function to modify the original argument?

Question 3 of 5

What is printed by this code?

void triple(int& n) { n *= 3; } int main() { int val = 4; triple(val); triple(val); cout << val; }

Question 4 of 5

Why is const & preferred over plain pass-by-value for a string parameter?

Question 5 of 5

What happens if you call func(10) where func is declared as void func(int& n)?