Pass-by-Value vs Pass-by-Reference
Compare semantic and performance differences between passing by value and by reference.
Pass-by-Value vs Pass-by-Reference is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Pass-by-Value Recap
When you pass an argument by value, the function gets a copy. Changes inside the function do not affect the caller.
void increment(int x) {
x++; // modifies the copy
}
int n = 5;
increment(n);
std::cout << n; // still 5Pass-by-Reference
Pass by reference using & in the parameter list. The function works directly on the caller s variable.
void increment(int& x) {
x++;
}
int n = 5;
increment(n);
std::cout << n; // now 6Why Pass by Reference?
Two reasons:
- Avoid copying large objects (performance)
- Modify the caller s variable (semantics)
Const Reference: Best of Both
A const reference avoids copying without giving the function permission to modify. Use it for non-trivial input parameters.
void printName(const std::string& name) {
std::cout << name;
}Rules of Thumb
For function parameters:
- Cheap to copy (int, double, pointer) → pass by value
- Expensive to copy and read-only →
const T& - Function modifies caller s data →
T&
Pass-by-Pointer
Pointers also let you modify the caller s variable — but the caller must take the address explicitly with &, making the intent visible.
void increment(int* x) {
(*x)++;
}
int n = 5;
increment(&n);Reference vs Pointer Parameter
References are nicer when the value is always required. Pointers are appropriate when the argument is optional — pass nullptr to skip.
Returning by Value
Returning by value is the default. Thanks to RVO (Return Value Optimization) and move semantics, this is usually as fast as returning by reference — and safer.
Returning by Reference
Return a reference when the function returns an existing object that outlives the call (a member of a container, for example). Never return a reference to a local.
Mixing Pass Styles in Templates
Templates often use universal references (T&&) and std::forward to preserve value categories. We will cover this in the Move Semantics course.
Common Mistake: Slicing
Pass derived classes by reference (or pointer) to a base parameter. Passing by value slices off the derived part of the object.
class Base { /* virtual */ };
class Derived : public Base { /* members */ };
void takeByValue(Base b); // SLICES Derived!
void takeByRef(Base& b); // OKQuick Check
How should you pass a large std::string that the function only reads?
Recap
Pass-by-value copies; pass-by-reference shares. Use const T& for read-only big objects, T& when modifying the caller s data, and small built-ins by value.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Pass-by-Value vs Pass-by-Reference” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Pass-by-Value vs Pass-by-Reference”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Pass-by-Value vs Pass-by-Reference”?
Compare semantic and performance differences between passing by value and by reference. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Pass-by-Value vs Pass-by-Reference” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why References Aliases for Variables
- Pass-by-Value vs Pass-by-Reference
- const References and When to Use Them
- Reference Lifetime and Dangling References