Sub Programs - Lakelands Computing

Title
Go to content
Sub Routines
Sub routines help avoid repeating your code. They can save you time and simplify your code. There are two types of sub routines we need to know about (at GCSE level): Procedures and Functions.

Procedures are sets of instructions that are stored under one name. When you want the program to run that whole list of instructions you just use that name. You call the procedure using its name.

Scratch is full of pre-built procedures - consider the "say for 2 sec" command. When it runs it probably does the following instructions (I'm not sure exactly what is coded behind the command):

  • Calculates where the top of the sprite is on screen
  • Works out what size speech bubble it needs (probably by counting characters (letters etc))
  • Draws the speech bubble
  • Adds the words you typed into bubble
  • Checks the current time
  • Calculates the time to remove the bubble
  • Removes all the above displayed items

Python also has pre built procedures - print() etc. You can also create your own in both languages.

Functions are similar to procedure with two key differences:
  • Functions always return a value procedures can return a value but don't always.
  • Functions always take at least one parameter (values passed into the function)

What that means is when you run a function it will always give you something (a value) at the end. A procedure to count the number of days to a birthday would display the answer - a function to do the same thing would give you the answer to store or use elsewhere  (it could display it to). To learn more about how functions work - look at this python skills page.


All Text copyright Lakelands Academy & Mr T Purslow 2020.
All images copyright free / creative commons unless otherwise stated.
You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
All Text copyright Lakelands Academy & Mr T Purslow 2020.  All images copyright free / creative commons unless otherwise stated. You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
All Text copyright Lakelands Academy & Mr T Purslow 2020.  All images copyright free / creative commons unless otherwise stated. You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
Back to content