Stage 1 · Lesson 4 of 8
Functions and references
A has a return type and typed parameters, and it must be declared before it is called. By default, arguments are copied (), so a function that changes its parameter changes only its own copy. Use , as in void addBonus(int& score), when the function should change the caller's variable.
Use a const reference, as in const std::vector<int>& v, to read a large object without copying it. That second form is how you pass strings and containers almost everywhere. A reference is simply another name for an object that already exists, and it must be tied to that object when it is created.
Never return a reference to a , because the local is destroyed when the function returns.
A question to check yourself, an AI practice session and 3 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with
- CourseFreeLearnCpp.com
LearnCpp.com · Beginner
Free, current and careful: chapters 0 to 16 take you from setup to references and std::vector