Separating logic from input and output means the calculation lives in a function that receives values through parameters and returns a result. The main program is the only place that asks the user for values and shows the answer.
This lesson closes the programming structure module. It combines parameters, returned values and scope in one habit that makes programs easier to test.
Why split the program this way?
Compare two ways of calculating an average. In the first, the calculation asks for numbers, adds them and prints the result, all in one block. To test it you have to type numbers every time.
In the second, the function only calculates. You can call it with four different sets of values in four lines, check the results, and use it again later.
A useful rule: a function takes values in through parameters and sends a value out through RETURN. The main program does the talking to the user.
Worked example
A fictional class test has three scores. Write the program so the logic is separate.
FUNCTION Average(A : INTEGER, B : INTEGER, C : INTEGER) RETURNS REAL
RETURN (A + B + C) / 3
ENDFUNCTION
DECLARE S1 : INTEGER
DECLARE S2 : INTEGER
DECLARE S3 : INTEGER
INPUT S1
INPUT S2
INPUT S3
OUTPUT "Average: ", Average(S1, S2, S3)
Suppose the user enters 70, 65 and 81. The main program passes S1, S2 and S3 as arguments.
| Step | A | B | C | Value |
|---|---|---|---|---|
| Call Average(70, 65, 81) | 70 | 65 | 81 | not yet |
| A + B + C | 70 | 65 | 81 | 216 |
| 216 / 3 | 70 | 65 | 81 | 72 |
| RETURN | 72 |
The output is Average: 72. Because the function has no INPUT or OUTPUT, you can test it with a table of chosen values:
| Call | Working | Expected result |
|---|---|---|
| Average(70, 65, 81) | 216 / 3 | 72 |
| Average(50, 60, 70) | 180 / 3 | 60 |
| Average(0, 0, 0) | 0 / 3 | 0 |
| Average(100, 100, 100) | 300 / 3 | 100 |
Each row can be checked by hand. That is the benefit: you test the logic once and trust it wherever it is called.
Which mistake catches students most often?
Mistaken code:
FUNCTION Average() RETURNS REAL INPUT A INPUT B INPUT C OUTPUT (A + B + C) / 3 ENDFUNCTION
This function has three problems. It asks for its own input, so you cannot test it with chosen values.
It outputs instead of returning, so a statement such as Result ← Average() receives nothing. It also uses A, B and C without declaring them.
The correction is the version above: values arrive as parameters, the answer leaves through RETURN, and the main program handles INPUT and OUTPUT.
The exception is when a question tells you directly to display something inside a procedure. Follow the question, and keep the habit for everything else.
Self-check
1. State the result of Average(90, 80, 70).
Show answer
90 + 80 + 70 = 240, and 240 / 3 = 80.
2. Give two reasons the function should not contain INPUT.
Show answer
You cannot test it with chosen values without typing them each time, and it cannot be reused where the values already exist in the program. It is also harder to read because it does two jobs.
3. Which part of the example program is the logic and which part is input and output?
Show answer
The function Average is the logic. The three INPUT lines and the final OUTPUT line are the input and output, and they stay in the main program.
Where does this lead next?
You have now covered the full set of structure skills in this module. Put them together in the programming structure practice set, where each question routes back to the lesson it tests. The safe Python reasoning sandbox is a good place to try the test table above as real code.
If you want a teacher to review code you have written and show you how to restructure it, online one-to-one Computer Science tuition can start from your own programs.