Skip to main content
Module 3 · Lesson 2 of 28/26 in the course~14 min
Module lessons (2/2)

Passage by value and reference

Lesson preview5 hands-on exercises readyCourse C++ · Module 3 — Functions and References

In-memory copies vs aliases with the (&) reference operator. Change external parameters and use const T&.

What you'll learn in this lesson

  • Distinguish in memory passing by value (isolated copy) from passing by reference (T&, direct alias).
  • Modify external variables safely using T& parameters without pointer syntax.
  • Avoid expensive copies of std::string and std::vector by always applying const T& as read-only.

Overview

In C++ you have complete control over how much work the CPU does when passing each argument: you can pass a small copy by value or bind a name directly to the original cell with a reference (&).

By default C++ passes each argument by value, creating a copy in the stack frame of the called function. Changing the local parameter leaves the caller variable intact:

Sections covered in this lesson

  1. 11. Pass by Value: Isolated Copy
  2. 22. Pass by Reference (T&): Direct Alias
  3. 33. Passage for const T&: Zero-Copy Reads on Large Objects
  4. 44. Practical Decision Table
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 3 — Functions and References

Unlock "Passage by value and reference" and the full Course C++ curriculum

In-memory copies vs aliases with the (&) reference operator. Change external parameters and use const T&.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course C++ plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.