3 – Functions, Variables and Loops

🎯 Learning Objectives

Develop the Programming and DevelopmentAlgorithmsLiteracyInformation Technology Learning Strands:

  • Develop basic understanding of structured programming using functions.
  • Use, declare and assign variables.
  • Be able to use appropriate keyboard interrupt events.
  • Use arithmetic operators and loops.

End of Week 2 Scratch Code

This content is password-protected. Ask your teacher for the password if you missed last lesson.

📖 Learn It – Using Functions
  • Open up your Virtual_LOL_Cat script you were working on last lesson.
  • Functions are really important in coding.
  • A function is a named part of a program that performs some specific task.
  • In Scratch, Functions are called Blocks.
  • Imagine I wanted you to make me a cheese sandwich. I could give you a set of instructions such as:
    • Take two pieces of bread.
    • Apply butter to one side of each piece.
    • Place a slice of cheese on the butter side of one piece of bread.
    • Place the other slice of bread, butter-side down onto the cheese.
  • However, it’s much easier to just say “Make me a cheese sandwich” as you probably already have the instructions stored in your memory. You have a named instruction for performing the sandwich making task
  • Functions allow us to write some code, and then run that code just by calling the name of the function.
⌨️ Code It
  • Killing the pet is something we might want to do quite a lot. So it would make sense to create a Function for killing the pet, that we can use over and over again.
  • First we’ll need to disconnect the code to kill the pet, from the when space key pressed event.
  • Next we’ll create a new function.
    1. Click on My Blocks.
    2. Click on Make a Block.
    3. Name it KillPet
    4. Click on OK
  • A new define KillPet block should have been created. This is your named function that you can attach your code to.
  • Now we’ll use the function
    1. Attach your code to kill the pet to the new define KillPet block.
    2. From My Blocks drag your KillPet block onto the Scripts Area.
    3. Attach the KillPet block to the when space key pressed event.
▶️ Run It
  • Test your code to make sure it works like it did before. The pet should die when the space key is pressed.
📖 Learn It – Using Variables
  • Variables are another important concept in programming.
  • A variable is named container that is made up of two things:
    1. Some data that is stored (like numbers or letters)
    2. A name that links to that data.
  • You have variables stored in your memory, even though you might not have realised it.
  • If I ask you what your name is, you can tell me. Your name is a variable.
  • If I ask you what your age is, you can tell me. Your age is also a variable.
  • key thing to remember about variables, is that they can change.
  • You could legally change your name to “Ima Coder”, and your age increments by 1 every time you have a birthday.
⌨️ Code It
  • We’re going to use a variable to represent how hungry our pet is.
  • The variable will have a name – PetHunger
  • The variable will store some data – 0 to represent not hungry, up to 100 to represent starving.
    1. Click on the Variables block.
    2. Click on Make a Variable
    3. Name it PetHunger.
    4. Click OK
  • To finish off this section, you’ll need to create a new script to handle the PetHunger.
  • Add a new when greenflag clicked event.
  • Attach a set PetHunger to 0 block beneath it. We’ll add more to this part in the next lesson.
  • See the example below:
⌨️ Try It
  • We’re going to need some more variables for our virtual pet, so we might as well create them now.
  • Create variables to for PetTirednessPetThirst and PetHappiness.
  • Each variable needs it’s own when green flag clicked event.
    • PetTiredness should start at 0.
    • PetThirst should also start at 0.
    • PetHappiness should start at 100.
  • You should end up with something looking like the following:
📖 Learn It – Using Loops
  • Loops are the third fundamental concept in programming to learn today.
  • loop cause instructions to be repeated over and over again.
  • There are basically two types of loops:
    • Loops that repeat a set number of times (Definite Iteration).
    • Loops that repeat until a condition is met (Indefinite Iteration).
  • Imagine someone was giving directions. They might say something like:

“At the next four roundabouts take the second exit to go straight on.”

  • This is a loop that repeats four times. You take the second exit at each roundabout exactly 4 times.
  • They might have said this instead though:

“Keep taking the second exit at the roundabouts, until you go under the blue bridge.”

  • This loop repeats an unknown number of times, until the you go under the bridge.
  • In Scratch these types of loop are called the repeat and the repeat until loops.
⌨️ Code It
  • We’re going to use a repeat until to make our pet get hungry.
  • In Scratch you nest the code you want to repeat, inside the loop block.
  • Here’s the complete script code. You build it from the script you already have to set the Pet’s Hunger to 0
when green flag clicked
set PetHunger to 0
repeat until PetHunger = 100
    change PetHunger by 1
    wait 0.5 secs
KillPet
  • This code can be tricky to assemble, so have a look at the example image below, but do have a go at doing it yourself first.
▶️ Run It
  • Run your script to make sure that it works.
  • You should see the PetHunger variable increase on the stage.
  • When it reaches 100, the pet should die.
  • If it doesn’t work then go back over the instructions, then ask a friend and finally ask your teacher for help.
  • For testing purposes, we’ve used a wait period of 0.5 seconds. But this isn’t really practical for the real Virtual Pet. Change the wait time to something longer. You can choose any value you like.
  • Now that you have got your PetHunger loop to work, you now need to repeat this for, PetTirednessPetThirst and PetHappiness.

🏅 Badge It – Explaining it All

🥈 Silver Badge
  • Upload the script you have completed after following the notes above.
🥇 Gold Badge
  • To show your understanding of the script you have just finished and tested, you need to explain what the script does by adding some comments next to some of the key blocks.
  • key block is one of the following:
    • A loop, such as repeat until, repeat.
    • A variable assignment such as set.
    • An if statement.
    • A function definition/creation such as the custom block.
    • A function call.
  • To comment a key block, right click on the script window and choose Add Comment
  • To get this badge, you need to comment at least three key blocks with clear explanations showing good understanding in good English.
🥉 Platinum Badge
  • In addition to the above Gold, you have commented all key blocks showing excellent understanding and in good English.

Upload screenshots of all of your badge tasks to bournetolearn.com