GCSE · Computer Science · OCR · Spec J277

Maintainability: sub programs

Imagine typing the same chunk of code every time you need it, then finding a mistake in it. In six places. Better plan: write it once, name it, call it.

Computer Science · Subprograms

One named task, called twice

Press Next to step through. The highlighted line is the one running, and the panel shows the values in play at that step.

One very large program vs the same program split into subprograms

One very large programvsSplit into subprograms

Read across each row. The right-hand column is why programmers bother with subprograms at all.

Focus

Reading it

One very large program

One huge block. To find the part you want, you wade through everything.

Split into subprograms

Small chunks, each with a name and one task.

The insight

It's much easier to read small chunks of code than one very large program. That's the readability win.

Making a change

One very large program

The change goes somewhere inside one long block of code.

Split into subprograms

Make the change and test it in one subprogram. The rest of the program isn't affected.

Testing it

One very large program

You're testing a big block that does lots of jobs at once.

Split into subprograms

You test a subprogram that carries out a single task.

Finding mistakes

One very large program

A bug could be hiding anywhere in a lot of code.

Split into subprograms

Less code to look through in each subprogram.

Needing a task again

One very large program

Need it three times? Write the code out three times.

Split into subprograms

Write it once, then call it three times.

Working as a team

One very large program

One long block, with no separate sections to hand out.

Split into subprograms

Several programmers develop different subprograms at the same time.

Computer Science · Subprograms

Find the interface

Pick a part of the code, then choose which part of the interface it is.

Still to sort

Identifier (0)

The name the subprogram is known by.

Where the line is: It names the subprogram. It isn't a value going in or coming out.

Parameter (0)

A name in the brackets of the definition, ready to take a value in.

Where the line is: Parameters carry data IN. If it's what comes back, it's the return value instead.

Return value (0)

What the subprogram sends back out, written after return.

Where the line is: It carries data OUT of the subprogram, not in.

8 of 8 still to sort.

Two subprograms. (1) function discount(price, percent) … newPrice = price - price * percent / 100 … return newPrice … endfunction, called as sale = discount(40, 25). (2) function greet(name) … message = "Hi " + name … return message … endfunction.

Watch out: The name of a subprogram turns up in two places: where it is defined and where it is called. It's the identifier both times.

Computer Science · Subprograms

Is a subprogram sealed off from everything?

Ava's program calls a subprogram, totalPrice(price, quantity), from five different places. She spots a mistake inside it: it adds price and quantity when it should multiply them. She fixes that one line. The subprogram keeps the same name, still takes price and quantity, and still returns the total.

What does Ava's fix mean for the rest of her program? Pick the idea closest to what you think right now.
How sure are you?

Your turn to write

Make the case for subprograms

A team is writing a large program for a school's online shop. Explain how writing it as subprograms will help the team to maintain the program and to work on it together. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Small named chunks of code, each doing one job, that make a big program easier to read, fix, test and share.

What you need to know

  • A subprogram is a sequence of instructions that performs one specific task, and it has a name to identify it.
  • Structured programming means dividing a program into smaller sections, called subprograms, each designed to do one specific task clearly.
  • To run a subprogram, call it by name. Control jumps in, the task runs, and control comes back.
  • Any subprogram can be called multiple times, so its code is written once instead of being repeated.
  • Have a goA program needs the same delivery-charge calculation in six different places. Written as a subprogram, how many times is that calculation's code written out?

    Once. It is then called six times.

    Each place calls the subprogram instead of having its own copy, so the code is written once. Six copies is exactly the repeated code a subprogram saves.

  • Reading gets easier, because small chunks of code are much easier to read than one very large program.
  • Maintenance gets easier: a change can be made and tested in one place without affecting the rest of the program.
  • Testing gets easier too. A single task is simpler to test, and less code makes mistakes easier to find and fix.
  • On a large program, several programmers can develop different subprograms at the same time, which speeds up development.
  • Have a goFour friends are building a big game together. One says: "Only one of us can touch the code at a time, so we'll just take turns." What could they do instead?

    Split the game into subprograms and each develop different ones at the same time.

    Several programmers can work on separate subprograms at once, which speeds up development. Taking turns on one huge block throws that advantage away.

  • The interface decides how data passes in and out. It's made up of the identifier, the parameters and the return value.
  • That 'rest of the program isn't affected' promise holds while the interface stays the same.
  • Have a goA subprogram called shipping is called from ten places. A programmer renames it postage. Does the rest of the program carry on untouched?

    No. All ten calls still use shipping, so each one must change to postage.

    The identifier is part of the interface, and every call uses it. Only changes that keep the interface the same leave the rest of the program alone.

The big picture

A subprogram is a sequence of instructions that performs one specific task and has an identifiable name. Structured programming divides a program into these smaller sections, each doing one job clearly. That makes code easier to read, easier to maintain and easier to test; one subprogram can be called many times instead of repeating code; and several programmers can develop different subprograms at the same time. Data passes in and out through the interface: the identifier, the parameters and the return value. A change can be made and tested in one place, as long as that interface stays the same.

Key points

1Subprogram = a named sequence of instructions that performs one specific task.
2Structured programming = a program divided into subprograms, one task each.
3Subprograms make code easier to read, maintain and test.
4Write once, call many times: no repeated code.
5Several programmers can build different subprograms at the same time.
6Interface = identifier + parameters + return value. Keep it the same and a change stays in one place.

Worked example

Problem

A quiz program works out a percentage score in three places, each with its own copy of the calculation: maths = 18 / 20 * 100, then science = 30 / 40 * 100, then history = 12 / 15 * 100. Rewrite it so the calculation is a subprogram, and check what each call returns.

⚠ Watch out

Saying a change to a subprogram can never affect the rest of the program. The change stays in one place only while the identifier, parameters and return value stay the same. Change one of those and every call must change too.

🧠

Memory hook

Name it, call it, reuse it. Data goes in through the parameters and out through the return value: that's the interface.

✓

Check yourself

Name the three parts of a subprogram's interface, then give two reasons subprograms help programmers. (Identifier, parameters, return value. Any two: easier to read, maintain or test; less repeated code; several programmers at once.)

Flashcards

(11)
What is a subprogram?
A sequence of instructions that performs a specific task, with an identifiable name.
What is structured programming?
Dividing a program into smaller sections called subprograms, each designed to perform one specific task clearly and effectively.
Why does splitting a program into subprograms make it easier to read?
Small chunks of code are much easier to read than one very large program.
How do subprograms make a program easier to maintain?
A change can be made and tested in one place, and the rest of the program is not affected.
Why is a subprogram easier to test than a large block of code?
It carries out a single task.
Why are mistakes easier to find and fix in a subprogram?
There is less code to look through.
How does calling a subprogram save repeated code?
The code is written once and can be called as many times as it's needed.
How do subprograms speed up the development of a large program?
Several programmers can develop different subprograms at the same time.
What does a subprogram's interface determine?
How data is passed in and out of the subprogram.
What three things make up a subprogram's interface?
The identifier, the parameters and the return value.
Which part of the interface carries data into a subprogram, and which carries data out?
Parameters carry data in. The return value carries data out.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 3 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.