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.
main reads like a summaryLevel 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.
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.
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.
Step through a real call and watch the copy being made.
Switch between the two versions and step through each.
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.
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:
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.
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.
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.
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 it needs, what it does, what it hands back. If you need “and” to name it, split it.
Changes inside are lost unless you asked for a reference with &.
If it reads like plain English, the breakdown was right. If it is 200 lines, it was not.
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.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
What does this print?
And with one extra character?
Your main is 200 lines long. What does that most likely mean?
← back to the whole C++ track · stuck on anything? ask peter.