Subroutines concept

What is a Subroutine?

A subroutine is a named, self-contained block of code that is written once and can be used at any point in a program simply by writing its name. Because the block exists separately from the main flow of the program - it is described as "out of line" - it is only executed when it is explicitly called. The program jumps to the subroutine, runs it, and then returns to where it left off.

Subroutines are one of the most important tools in programming. Without them, any task that needs to happen more than once would require the same lines of code to be written out repeatedly - making programs longer, harder to read, and much harder to fix when something goes wrong.

Why Use Subroutines?

  • Avoiding repetition: code written once in a subroutine can be called as many times as needed from anywhere in the program.
  • Readability: giving a subroutine a meaningful name makes it immediately clear what that part of the program does.
  • Easier maintenance: if the logic needs changing, it is changed in one place rather than in every location where the code was duplicated.
  • Decomposition: large problems can be broken into smaller, manageable subroutines, each solving one part of the problem.

Subroutines in Practice

The two examples below contrast a program without a subroutine against the same program with one, to show clearly what a subroutine achieves.

The greeting message is needed twice, so the same lines appear twice. If the wording needed to change, both copies would need updating - and it would be easy to update one but miss the other.

OUTPUT "--------------------"
OUTPUT "Welcome to the quiz!"
OUTPUT "--------------------"

OUTPUT "Round 1 - Geography"
... (quiz questions) ...

OUTPUT "--------------------"
OUTPUT "Welcome to the quiz!"
OUTPUT "--------------------"

OUTPUT "Round 2 - Science"
... (quiz questions) ...

The greeting block is defined once as a subroutine called showWelcome. It is then called by name whenever it is needed. The definition and the calls are separate - the subroutine block only runs when explicitly called.

SUBROUTINE showWelcome()
    OUTPUT "--------------------"
    OUTPUT "Welcome to the quiz!"
    OUTPUT "--------------------"
ENDSUBROUTINE

showWelcome()            ← call: runs the block above
OUTPUT "Round 1 - Geography"
... (quiz questions) ...

showWelcome()            ← call: runs the same block again
OUTPUT "Round 2 - Science"
... (quiz questions) ...

Notice that the word showWelcome() on its own is enough to trigger the whole block. The program jumps to the definition, executes it, and returns to the next line.

Procedures and Functions

There are two specific types of subroutine: procedures and functions. A procedure carries out a task; a function carries out a task and also returns a value back to the part of the program that called it. Both are called in the same way - by writing their name - but a function's result can be stored in a variable or used in an expression. These are explored fully in the next two benchmarks.

 Key Takeaways

  • A subroutine is a named, "out of line" block of code. It only runs when it is called by writing its name in the program.
  • Subroutines eliminate repeated code - write the logic once, call it as many times as needed.
  • Using subroutines makes programs easier to read, test, and maintain: a change in one place applies everywhere the subroutine is called.
  • Subroutines support decomposition: a complex program can be divided into smaller, clearly named subroutines each handling one task.
  • The two specific types of subroutine are procedures (perform a task) and functions (perform a task and return a value) - these are covered in the following benchmarks.