C++ Interview Question

What is the difference between pass by value and pass by reference in C++?

Updated 2026-08-16 · Beginner friendly
Quick answer

Pass by value copies the argument into the function, so changes inside do not affect the original and copying can be costly for large objects. Pass by reference passes an alias to the original, so changes are visible outside and no copy is made. Use pass by value for small types, and pass by const reference for large objects you only read.

Key takeaways
  • Pass by value copies the argument, so changes inside do not affect the original.
  • Pass by reference passes an alias, so changes are visible and no copy is made.
  • Use small types by value and large read only objects by const reference.

Pass by value

void addOne(int x) { x++; }   // change is lost, x was a copy

Pass by reference

void addOne(int& x) { x++; }  // change is visible to the caller

The performance angle

Copying a large object like a big vector by value is expensive. Passing by const reference avoids the copy while still preventing modification, which is the common pattern for read only large parameters.

In the interview

The rule interviewers want is small types by value, large types by const reference. Saying use a plain reference only when you intend to modify the caller's object rounds out a complete answer.

Frequently asked questions

Why pass large objects by const reference?

It avoids the cost of copying a large object while still preventing the function from modifying it, which is efficient and safe for read only use.

What is move semantics?

Move semantics transfers resources from a temporary object instead of copying them, which avoids expensive copies for objects like large vectors.

Want the full C++ guide?

Read every C++ concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the C++ guide All C++ questions