C++20 sections · 1024 units
Open in Course

Passing by Reference

Avoiding copies

To avoid copying, pass by reference: void printVector(vector<int>& numbers). The ampersand means the function works with the original, not a copy. If you don't want modification, add const: void printVector(const vector<int>& numbers).

Speed of references with safety of preventing changes. I recommend const reference for most vector parameters. Fast, safe, and communicates that the function won't change the data.