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 — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Pass-by-Value vs Pass-by-Reference” lesson free?
Yes — the full text of “Pass-by-Value vs Pass-by-Reference” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
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