Using a FUNCTION in Scratch

A function is basically a series of steps that you teach the computer – it then learns the steps and can use them when you ‘call it’. The idea is you don’t need to continually tell the computer how to do something as it already knows.

In this case, we are going to teach the computer how to work out the percentage of a number.  The steps are:

  1. Take the number and divide it by 100
  2. Multiply that answer by the percentage you want
  3. Print out that answer

In Scratch, you can do this using the MAKE BLOCK function. You define the block and put in how many numbers you need. In the function, you define the steps and return the answer.

You then ‘call the function’ by adding the block to the code.

function

Using REPETITION in Scratch

REPETITION is a very useful function on coding and saves time rather than doing a long list of code.  The basic idea is that you can have a variable and then write code so the variable increases by 1 (or another number) until a certain number is reached.

Example: To get Scratch to work out the 5x table

Using pseudocode:

  1. Declare a variable called MULTIPLIER
  2. Make MULTIPLIER = 0
  3. Work out 5 x MULTIPLIER
  4. PRINT the answer
  5. Add 1 to MULTIPLIER
  6. Repeat steps 3 – 5
  7. STOP the code when MULTIPLIER = 12

This is how the code looks like in Scratch.

repetition