
Think it through together · A little story
A robot needs to calculate the class’s scores. The girl splits the task into “read the data,” “calculate,” and “display,” with each card responsible for one job.
Lay out the process
- Input functionGet the scores
- Calculation functionWork out the result
- Display functionShow the result
When splitting code into functions, first be clear about what input each function needs and what result it produces.
Clear boundaries let us check one small part independently. For example, a function that calculates an average does not also need to draw a beautiful interface.
01Why not write a large program entirely in main?
It may be fine when a small program is only a dozen lines long. But as a program grows, mixing input, calculation, decisions, and output makes it difficult to change.
At that point, we can split it by task.
02Find the tasks before writing functions
For example, “read 5 scores, find the average, then display the result” can be divided into:
Read the data
Responsible for getting the scores.
Calculate the average
Responsible only for the calculation.
Display the result
Responsible for showing it.
03How do you design a function’s inputs and outputs?
Start by asking two questions:
What does the function need to know?
That information may be suitable as parameters.
What should the function give back?
That information may be a return value or part of another clear interface.
04A simple example of splitting a task
double average(const int scores[], int count) {
long long sum = 0;
for (int i = 0; i < count; i++) {
sum += scores[i];
}
return static_cast<double>(sum) / count;
}
This function only calculates the average of a non-empty group of scores. It does not read data from the keyboard or print text.
05What does “one function does one thing” mean?
It does not mean that a function can contain only one line of code. It should have one clear responsibility.
Clear
findMaximum()
Clear
printResult()
Unclear
doEverything()
06How do functions fit together?
main can act like an overall coordinator, while functions with clear responsibilities handle each specific task.
07What practical benefits do functions offer?
Easier to read
Names explain the purpose of each part.
Easier to test
Test a function on its own.
Easier to change
One responsibility can be changed with less impact elsewhere.
Reusable
The same task does not need to be rewritten.
08When should you stop splitting?
If a piece of logic is very short, used only once, and becomes harder to follow when split out, it may not need its own function.
What you have learned
- The first step in splitting functions is to identify relatively independent tasks.
- Parameters describe the input a function needs; return values describe the results it produces.
- A good function usually has one clear responsibility.
maincan organize the overall flow while smaller functions handle specific tasks.- Splitting functions should support correctness, readability, testing, and reuse—not simply increase their number.
Topic complete: Next, we move on to “Recursion and the Call Stack.”
Your turn to try
If the displayed color is wrong, should you first check the score-reading function or the display function?
Have you thought it through? Open for an explanation
Start by checking the display part and the data passed to it. Clear responsibilities help narrow the search, but you should still verify that the input is correct.