Click to see the rest of the programming section :
As you move through your lessons on programming you will find that you use more subroutines to save yourself time. As programmers it means we write fewer lines of code, which is brilliant but it also saves the computer time and effort too.
The great thing about a subroutine is that the computer doesn’t have to assign any memory to it until the program uses the code, and when the subroutine is over any memory that was used is recycled freeing up the space for something else.
At GCSE we study procedural programming and so the only two subroutine types that we need to be concerned about are procedures and functions. All subroutines are a block of code that has a name and sits outside of the main program – procedures use this technique to perform a task that doesn’t need to send any data back.
Imagine that you have been asked to tidy your room – you don’t need to report back on the success of the task, or send back the percentage of the room that is now tidy, it just needs to get done. In the same way, a procedure completes a task and then the program carries on. In pseudocode, this might look like:
OUTPUT “Please choose an option:”
OUTPUT “A: Deutsch”
OUTPUT “B: English”
OUTPUT “C: Espanol”
Note that here, there is no place to input or any validation. But this procedure will also never run until we add the line seeMenu() to the main code, and that’s because subroutines are like dogs and don’t run until they are ‘called’.
The Python program below has implemented this algorithm, but there are some things missing… Calling the procedure! We do this by adding the name of the subroutine followed by brackets. Try expanding the program so that the menu appears when the program is run. To run the program in the browser, press Ctrl + Enter.
Find this page helpful? Share the love on your social media mentioning @TeachAllAboutIT and we’ll enter you in our monthly draw to win a gift voucher for any product on the site!