Skip to content
IGCSE·Tuition
Computer Science · Lessons

Separate logic from input and output

Your program works when you type values in, but the moment you want to test it with different numbers it becomes a chore.

On this page
  1. Why split the program this way?
  2. Worked example
  3. Which mistake catches students most often?
  4. Self-check
  5. Where does this lead next?

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.

StepABCValue
Call Average(70, 65, 81)706581not yet
A + B + C706581216
216 / 370658172
RETURN72

The output is Average: 72. Because the function has no INPUT or OUTPUT, you can test it with a table of chosen values:

CallWorkingExpected result
Average(70, 65, 81)216 / 372
Average(50, 60, 70)180 / 360
Average(0, 0, 0)0 / 30
Average(100, 100, 100)300 / 3100

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.

Questions people ask

What does it mean to separate logic from input and output?

The logic is the calculation or decision, written as a function that takes parameters and returns a result. Input and output, which read from the user and show results, stay in the main program. The function then does one job and can be tested without anyone typing.

Why should a function not contain INPUT or OUTPUT?

If a function asks for its own input, you cannot call it with chosen test values. If it outputs instead of returning, other code cannot use the answer. A function that takes parameters and uses RETURN can be reused anywhere.

Will the exam ask me to write functions this way?

Questions often give you a small task and ask for a function or procedure, so the habit helps. Follow the wording of the question: if it says display, use OUTPUT, and if it says return, use RETURN. Check your exam year's syllabus for the exact pseudocode conventions.

Updated:

Your next step

If your programs work but are hard to change or test, a one-to-one teacher can go through your own code and help you split the calculating part from the asking and showing part.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service