7. Subroutines

What is a Subroutine?

A subroutine is a section of code that can be re-used several times in the same program. It is separate from the main code and has to be ‘called’ upon. In a game of Mario you could imagine a subroutine as the part of the level that is reached by travelling down a pipe. It is away from the main level / program and you once you have gone through it you return to the program again (you can also re-visit it several times). Subroutines are designed to be repeated and they have three key benefits:

  1. Subroutines make programs more readable.
  2. They reduce the duplication of code.
  3. Complex problems are broken down into smaller chunks.

There are two types of subroutines, procedures and functions. A procedure just executes commands, such as printing something a certain number of times. A function produces information by receiving data from the main program and returning a value back to the main program. For example, a function could take the radius of a sphere from the main program and then calculate a sphere’s area and return the value of the area back to the main program. A function generally requires parameters to work – these are the values to be transferred from the main program to the subroutine.

Click the links below for more information on procedures and functions:



When you have read the pages above and tried the practice task(s) on each page then attempt these:

End of Section Practice Tasks