cadence_learn
the_c++_track / level 09 of 15 ▶ 17 min

in, one job, out.

Level 2’s named pieces, typed out. Step through a real call and watch the copy being made — then find out why “my function does nothing” is the most common complaint in the course.

after this level you'll be able to
  • Write a function that takes something and hands something back
  • Explain why changes inside a function are usually lost
  • Break a program into functions so that main reads like a summary
level 2, in c++

the named pieces, for real

Level 2 ended with this: the bottom row of your breakdown is the list of functions you are going to write. Here is what one looks like when you actually write it.

int twice(int n) { // hands back an int · is called twice · needs one int return n * 2; // hand this back to whoever called } // somewhere in main: cout << twice(21); // prints 42

Three parts, exactly as level 2 described them. What it hands back (int, on the left). What it needs (int n, in the brackets). What it does (the body). If a function needs nothing, the brackets are empty. If it hands nothing back, the type is void.

where to put it

Above main. C++ reads top to bottom and will not call something it has not met yet — putting a function below main gives you the “undeclared identifier” error from level 3. There is a way around that (a prototype) and your textbook will cover it; until then, just put them above.

◈ ask an ai about this

“How do I write my own function in C++? Explain parameters, return values and void with a small example.”

chatgpt ↗ claude ↗

the thing that surprises everyone

your function gets a photocopy

one ampersand, two completely different outcomes
BY VALUE — void addOne(int x) main’s box 5 COPIED the function’s own 5 → 6 the function changes ITS copy main’s box is still 5. The change is thrown away. BY REFERENCE — void addOne(int &x) main’s box 5 → 6 SAME BOX just another name no second box the function changes THE original main’s box really is 6 afterwards. One ampersand is the entire difference, and it decides whether the caller’s data survives. Verified: 5 and 6.
By default C++ hands a function a photocopy. Scribble on the photocopy and the original is untouched — which is safe, and is why “my function did nothing” is such a common first complaint.

Step through a real call and watch the copy being made.

▤ lab 08 · the call

Switch between the two versions and step through each.

MAIN
v5
ADDONE
x
Press step. Watch carefully at the moment the function is called.
“my function does nothing”

This is the complaint, almost word for word, and this is the cause. The function ran perfectly — it changed its own copy, then the copy was thrown away when it finished. Nothing is broken; the default is simply a copy. Use & when you genuinely want the original changed, and prefer returning a value when you can.

◈ ask an ai about this

“Why does my C++ function not change the variable I passed in? What does the & in int &x actually do?”

chatgpt ↗ claude ↗

why bother

functions are how you stop drowning

A first assignment fits in main. The third one does not, and the difference between a person who is enjoying this course and one who is not is usually whether they broke the work into named pieces.

Your catalog asks for programs that are “correct and maintainable” and puts its emphasis on top-down design. This is where that gets typed. Instead of one 200-line main, you write:

int main() { double total = readReceipts(); double taken = readSales(); printProfit(taken - total); return 0; }

Four lines, and you can read what the program does without reading how any of it works. That is the bake sale tree from level 2, typed out. Each name is a promise, and each function is small enough to test on its own — which is the only way level 11’s testing is practical at all.

a rule of thumb worth keeping

If a function does not fit on your screen, it is doing more than one job. If you cannot name it without using the word “and”, same problem. Both are level 2 telling you it has not been broken down enough yet.

do this together

name the pieces, then write one

☶ two people · 20 minutes

Break a task into functions on paper before writing any of them.

person a

Pick a small program — a tip calculator, a marks average, a shopping total. Write only the names and what each one takes and hands back. No bodies.

person b

Write main using only those names, as if they already exist. If main does not read like plain English, send the list back.

Only once main reads well do you write the bodies — one each, in silence. They will fit together, because the names and the types were agreed first. That is what a team does, and it is why functions exist at all.

what to keep

three things worth remembering

01

In, do one job, out

What it needs, what it does, what it hands back. If you need “and” to name it, split it.

02

A copy by default

Changes inside are lost unless you asked for a reference with &.

03

main should read like a summary

If it reads like plain English, the breakdown was right. If it is 200 lines, it was not.

check yourself

3 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself3 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

What does this print?

void addOne(int x) { x = x + 1; } int v = 5; addOne(v); cout << v;
02

And with one extra character?

void addOne(int &x) { x = x + 1; } int w = 5; addOne(w); cout << w;
03

Your main is 200 lines long. What does that most likely mean?

answered: 0 of 3right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →