Topic 8 Β· Programming Β· 8.12

Subroutines Explained Simply

A subroutine is a named section of code that performs one task. The main program can call it whenever that task is needed.

Invitation

Why write the same instructions again and again?

Large programs often repeat the same task, such as displaying a menu, checking a password or calculating a total.

A subroutine stores the instructions for one task under a name. The program can then call that name whenever the task is needed.

Write the task once. Give it a name. Call it whenever it is needed.
Figure 1
Avoid repeated code
Display menu code
Display menu code again
Display menu code again
↓
CALL DisplayMenu
Big Idea

The main program pauses, calls the subroutine, then returns

When a subroutine is called, the main program temporarily moves to that named section of code.

After the subroutine finishes, control returns to the instruction immediately after the call.

Call β†’ run the subroutine β†’ return to the main program.
Figure 2
The call-and-return journey
Main program
↓ CALL
Subroutine runs
↓ RETURN
Main program continues
FutureLogic Bridge

A subroutine is like a reusable recipe card

Look at Figure 3. The main program reaches the instruction CALL MakeToast.

It follows the call to the gold recipe card, completes each instruction, then returns to the main program.

The recipe does not need to be rewritten every time toast is required. The program simply calls its name again.

Follow the numbers: 1. Call the recipe. 2. Complete the task. 3. Return and continue.
Figure 3
Follow the reusable recipe
1Main program: CALL MakeToast
↓
2MakeToast: bread β†’ toaster β†’ wait
↓
3  Return to the next instruction
The same recipe card can be called many times.
Worked Example

Create and call a procedure

This subroutine displays a welcome message. It is defined once and then called from the main program.

PseudocodeProcedure
PROCEDURE WelcomeMessage
  OUTPUT "Welcome"
  OUTPUT "Choose an option"
ENDPROCEDURE

CALL WelcomeMessage
OUTPUT "Program continues"
PythonSame structure
def welcome_message():
    print("Welcome")
    print("Choose an option")

welcome_message()
print("Program continues")

Follow the program

CallThe main program calls the named subroutine.
RunThe two messages inside it are displayed.
ReturnThe final output in the main program runs.
Output order: Welcome β†’ Choose an option β†’ Program continues.
Exam Tip

Know the difference between defining and calling

Defining a subroutine means writing and naming the instructions it contains. Calling it means telling the program to run those instructions.

Exam-ready answer:
β€œA subroutine is defined once but can be called many times from the program.”

Subroutines make programs easier to read, test and maintain because each section has one clear purpose.

Common Mistake

Writing the subroutine but never calling it

Defining a subroutine does not automatically run it. The main program must call it.

Incorrect idea: β€œThe procedure runs because it has been written.”
Correct: β€œThe procedure runs only when it is called.”

In Python, remember the brackets when calling a function: welcome_message().

Summary

The subroutine pattern

Named sectionA subroutine contains instructions for one task.
DefineWrite the subroutine and give it a meaningful name.
CallTell the program when to run it.
ReturnControl goes back to the main program afterwards.
ReuseThe same subroutine can be called many times.
PurposeReduce repeated code and improve maintainability.