What are procedures and functions?
What are procedures or functions?
In programming a procedure or function is a defined piece of code that can be run multiple times.
Similar to functions in mathematics, in programming functions have to have an input and an output, whilst a procedure can just run code without having any input or output.
Start a unique learning programme!
- Weekly programme for each school year
- Worksheets sent direct to your inbox
- Keeps your child's learning on track
Procedures and functions in KS1
Procedures and functions are highly advanced topics and bridge the gap between intermediate and advanced programming techniques.
Your child is unlikely to be introduced to procedures in KS1 as they are not part of the current computing curriculum at early stages and require an understanding of a programming language.
Procedures and functions in KS2
The concept of procedures / functions will not be introduced until at least Year 5 when your child has a good grasp of basic programming principles and is able to use sequences, loops and conditions effectively.
A procedure is a piece of code that can be run over and over again, which is useful as it prevents us from having to re-write the same code multiple times.
A function is also a piece of code that can be run over and over again, with one key difference: a function should have at least one input and an output. An input to a function is called an argument and an output is called the return.
There is no set way that functions are introduced in the curriculum, however they will usually be introduced using a visual programming language such as Scratch and will allow students to simplify the piece of code they are writing by breaking it down into small, bite-sized chunks.
KS2 procedures and functions coding project
For example: I write a game and want to move my character right when the right arrow key is pressed, or alternatively when the D key is pressed. If I do not use a procedure then I will have to write the same code twice, once to deal with the right arrow key being pressed and once to deal with the D key being pressed. The solution is to write a procedure that can be run multiple times in order to move the character right.
In order to demonstrate this we are going to look at some Scratch code. Before we look at the actual code, however, let's learn about what some of the Scratch blocks do:
Now let’s have a look at some code examples, one that doesn’t use a procedure and repeats the same code multiple times and one that uses a custom block to move our character to the right.
First let’s look at an example that repeats the same code multiple times:
As you can see, in this code we have repeated the same code multiple times. This isn’t very efficient – if we wanted to add in another key that allowed us to move our character right, we would have to repeat the same code again.
We can solve this problem by using a procedure (or, as it's called in Scratch, a custom block). Let’s have a look at what a function might look like in Scratch:
As we have used the Move Right custom block, the repeated code is only one line now, instead of two lines as in the previous example. Althugh we still have the same number of lines of code, by using a custom block we have done two things:
- We have split up our code into smaller sections, meaning that instead of trying to solve one big problem, we are able to solve a number of smaller problems.
- We have made it easier to change the way that our character moves right. Instead of having to change our code in two places, if we want to update our procedure we can just change it inside our custom block.
In our final example I have updated our Move Right custom block so that we also play a sound whenever the character moves, and if we reach the end of the screen we bounce. Notice that I haven’t had to change anything in our main block of code; simply changing the code in the function has allowed us to change what happens when both the right arrow, or the D key is pressed:
Procedures and functions in primary school
Please note that the following guideline for what is taught in each primary year is for guidance only and may not be representative of the way computing is taught at your child’s school.
The concept of procedures is introduced and students will have to write basic procedures in a visual programming language such as Scratch.
The concept of input and output are added to students' understanding of procedures to create more complex functions.