Coding/Learn/cpp/pass by reference
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnPass by Value vs Pass by Reference (&)
14 / 20 0%
Lesson 14 of 20
5 mins read

Pass by Value vs Pass by Reference (&)

PreviousNext Lesson
In Plain English

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.

The Mental Model

"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.

PointCore Rules to Remember

Point star
Zero Copy Overhead: Pass by reference avoids copying large objects, saving CPU time and RAM.
Point star
Direct Mutation: Allows functions to modify caller variables directly (like swapping two numbers).
Point star
const Reference: Use const std::string &str to pass strings efficiently and safely.

Common Beginner Traps & How to Fix Them

Mistake:Expecting pass-by-value functions to mutate variables

Why it happens: Assuming function parameters automatically modify the original variable.

How to fix: Add the & symbol to the parameter type: void update(int &val).

Pro Tip: In professional C++, virtually all non-primitive objects (strings, vectors, classes) are passed by const reference (const T &)!

Live Interactive Example

Hit Run Code to see it live
Swapping Two Numbers by Reference
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: Because parameters use int &a and int &b, x and y are swapped directly.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Double a value by reference

  • PointWrite void doubleValue(int &n) { n *= 2; }
  • PointIn main(), declare int score = 50;, call doubleValue(score);, and print "Score: 100".
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates