Passing Arguments in C++

From the C++ cheat sheet · Functions · verified Jul 2026

Passing Arguments

By value, reference, const reference, and pointer.

cpp
// Quick Reference
void byValue(int x);          // copy
void byRef(int& x);           // can modify caller's variable
void byConstRef(const std::string& s); // no copy, read-only
void byPtr(int* p);           // pointer
💡 Pass large objects by const& to avoid an expensive copy while keeping them read-only.
⚡ Use a non-const reference (int&) when the function must modify the caller's value.
📌 Prefer references over pointers unless the argument is genuinely optional (nullable).
🟢 By-value passing is safe and simple for small types (int, double, pointers).
parametersreferences

More C++ tasks

Back to the full C++ cheat sheet