Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Lesson 9: Parameter Passing – By Value a | C++ Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. C++ Course

Lesson 9 of 45

·C++

Lesson 9: Parameter Passing – By Value and By Reference

9 min

Course progress20%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • Copy semantics for value parameters
  • Reference parameters
  • Const references for read‑only arguments
  • When to choose each method

Parameter Passing – By Value and By Reference

Understanding how arguments are passed influences performance and semantics. By value copies the argument, invoking copy constructors for class types. By reference (T&), the function works directly with the original object, avoiding a copy. Adding const to a reference (const T&) allows read‑only access without copying.


Example: Demonstrating Copy vs Reference

struct Widget {
    Widget() { std::cout << \"Widget constructed\
\"; }
    Widget(const Widget&) { std::cout << \"Widget copied\
\"; }
    ~Widget() { std::cout << \"Widget destroyed\
\"; }
};

void byValue(Widget w) {
    std::cout << \"Inside byValue\
\";
}

void byRef(Widget& w) {
    std::cout << \"Inside byRef\
\";
}

int main() {
    Widget w;
    byValue(w); // copy occurs
    byRef(w);   // no copy
    return 0;
}

Output clarifies when the copy constructor runs. For large objects, prefer pass‑by‑reference to avoid expensive copies.

Tip: Use std::move to transfer resources when you intentionally want to move from a value.


Note

Never return a reference to a local variable; it will dangle after the function exits.
Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from C++ CourseBrowse activities
Previous lessonLesson 8: FunctionsContinue to lesson 10Lesson 10: Function OverloadingBack to C++ Course curriculum