What are functions?

Functions are created by us to compile sequences of code that we can then call upon whenever we want that sequence to be executed.

They are a fantastic way for us to manage our inputs (information received into the program) and outputs (information that is sent out from our computer program) and ensure our code is organised, reusable and easier to test.

  

In our Arcade game, it may be useful for us to create functions for tasks that we want to repeat at various points within the game.

For example, if we want the same theme tune to play at the start of our game, but, also as we begin each level, we can create a function and called it "themeTune". "themeTune" can then include our sequence of musical notes.

When we want to include the tune, we then just call on "themeTune" at the appropriate location in our program, without the need for rewriting that musical sequence all over again.   

Did you know?

There are some coding languages that are known for primarily using functions. One example is a language called Haskell, a functional programming language that is named after the US mathematician Haskell Brooks.  

Try this out at home with a partner:

What you will need:

  • Pen
  • Paper

You are player A and your partner is player B. 

1) Observe the table below:

2) Player A decides on a mathematical function, like the examples above or you can make your own. This function will be carried out on whatever input is received from Player B. 

3) Player A should write down the function or bit of processing and set it aside, out of sight of Player B.

4) Player B then gives Player A a number to process.

5) Player A processes the number, using their function, and returns an output to Player B.

6) Repeat steps 4 and 5 three times. 

7) Player B must then try to figure out and state what function or bit of processing they think Player A is using on the input to produce the given output. 

8) If Player B is successful then swap roles. 

9) If Player B isn't successful, then repeat steps 4 &5 until they are. The goal is for Player B to figure out what function or bit of processing Player A is using in the fewest number of rounds of input/output possible.

10) Try to complete 3 rounds each or more if time permits.

So, now that we have wrapped up this game, we are ready to begin a brand new game in our next lesson. Your home challenge involves getting ready for the new game.

The DreamSpace team want you to plan your next game using the Sprite Planning Sheet and the Game Planning sheets which you can find in the PDF below.