Notes

Thoughts, write-ups, and things worth remembering.

Pass by value, reference, pointer

#cpp#fundamentals#pointers Updated Sep 16, 2026

Three ways to hand a function an argument — each answers “can the function change my variable, and does it copy anything?” differently.

void byValue(Node n);            // copies n. Changes inside don't touch the caller's copy.
void byRef(Node& n);             // no copy. Changes inside DO affect the caller's variable.
void byConstRef(const Node& n);  // no copy, can't modify - the default for big objects
void byPointer(Node* n);         // no copy, can modify, can be nullptr (which & can't be)

Default rule of thumb

  • Small, cheap-to-copy types (int, float, bool) → pass by value, don’t overthink it.
  • Big objects you’re only reading → const&, skip the copy.
  • Need to modify the caller’s variable → & (or * if “no value at all” is a valid case).

Const keyword
References vs pointers
Pointer basics