Scratch Advance
Scratch Advance
Scratch Advance
when clicked
Variable
text
draw a triangle
My Blocks Add an input Add an input Add a label
number or text boolean
Cancel OK
112
The program
Functions are useful when you need to This is your triangle,
repeat any section of code more than once, drawn 12 times in a circle.
but they are really powerful when you use
them several times in the same program.
Here, we will use the triangle function over
and over to make some beautiful designs.
when clicked
Make sure to clear
erase all the stage before you
start so that you have
go to x: -80 y: 0 a blank canvas.
point in direction 90
This is the same triangle,
repeat 12 drawn 8 times in a circle.
go to x: -200 y: -200
113
Functions with
parameters
What happens when you want to do something over and
over, but not in exactly the same way every time? Then
you need a function that you can change—a function with
parameters. Parameters are a kind of variable that make
your function work differently in different situations.
Make a function
with parameter block
We are going to make a block called “draw a square.”
Add the code below to the “define”
Select Make a Block and name your function, then
block so it can draw a square.
select “Add an input” and name the input “size.”
This input will be our parameter and it will change
the size of the square. define draw a square size
pen up
text
Cancel OK
When you use your “draw a square”
function, there is a space for you to
Click “Add an input” to add a
fill in a value for the parameter “size.”
parameter to your function.
114
The program
when clicked
Now we are going to use our function
with parameter block to make a pattern
erase all
of different squares with variable
sizes. Each time we want to make a
go to x: -30 y: 90
new square, all we have to do is use
our function and add the size of point in direction 90
the square that we want to draw.
repeat 10
This function with
draw a square 20 parameter block
draws a square with
move 60 steps sides 20 pixels long.
draw a square 30
turn 40 degrees
This code draws three different-sized
squares. Instead of making three
different functions, though, you only
go to x: -200 y: -200
need one function with a parameter.
Now try...
Try changing the value
of the parameter “size”
and see what happens
to the pattern. How big
a square can you make?
How small a square can
you see?
115
Decomposition
Even if you know what you want your program to do, it’s not
always clear what blocks you’ll need or how you will get the
computer to complete the actions you want. That’s why you
need decomposition. When you decompose a problem,
you break it down into smaller pieces to see more easily
how to program each part.
The algorithm
Here’s an algorithm for a program that takes words
and spells them backward. It needs to be broken
down further before it can be coded. Can you
identify which steps can be done with a single block
and which parts might need to be decomposed?
116
Decomposing your algorithm
There isn’t a single block in Scratch that spells a word
backward, so this step would benefit from decomposition.
Think about this step and what smaller steps within it
need to happen. Also, since the step appears twice, you
could make a function with parameters to save time later!
Start
You can use the
Pass the answer answer to both
into a function questions as a
called “spell parameter in a
new function.
backward”
Ask user
for their
A variable can store
favorite
The function the number of letters
animal in the word.
counts how
many letters
the answer has
End
117
Making new blocks Code Costumes Sounds
The decomposed steps need both a
My Blocks
variable and a new function. You can Motion
create those in the Blocks Palette under Make a Block
Variables and My Blocks. Make sure to Looks
say Backward, that’s spelled: for 1 seconds This Variable block takes the
parameter “word” and counts
the number of letters in it.
repeat number of letters
118
This Sensing block makes
The program the program wait until the
user has typed an answer.
when clicked
Now you can build the This block will call
whole program. When ask What’s your favorite animal? and wait the function that you
it runs, the sprite will defined earlier using
ask the user to enter spell backward answer the answer from the
question above it.
answers that can then
be spelled backward. ask What’s your favorite food? and wait
numLetters 0 numLetters 5
First, the program will ask the user for their The program counts the number of letters in your
1 favorite animal and a box will appear. Type your 2 answer. If you picked “horse,” which has five
answer in and click on the check mark. letters, you will see “5” appear on the stage.
The sprite says each letter of the word from last to Once the function has run, the code returns to the
3 first. It stops when the variable gets to 0. 4 main program and asks you the next question.
119
The program when clicked
These scripts make a sprite change costume, go to x: 0 y: 0
color, and size. However, the program is much
longer than it needs to be. Take a look at it and point in direction 90
see if you can find any repeating patterns that
switch costume to costume1 ▾
you could replace with a function.
set size to 100 %
go to x: 0 y: 0 wait 2 seconds
stop all ▾
Pattern matching
Sometimes pieces of code that work for one thing will also
work for other similar programs. Looking for patterns in
scripts can help you find code that can be reused or made
into a function that you can use whenever you need it.
120
Make a function
There are three places in the scripts where
the sprite is sent back to the center of the Define the “reset”
stage, turned the right way, and reset to function by adding all
its original costume, size, and color. Can you the code that repeats
see where? They can all be replaced by a in all three scripts.
single function.
define reset
Code Costumes Sounds
My Blocks
Motion
go to x: 0 y: 0
Make a Block
Looks
point in direction 90
reset
Sound
Make a new switch costume to costume1 ▾
block that
will hold the Name the block set size to 100 %
Events
reused code. something that
will remind you clear graphic effects
Control what it does.
Sensing
Make a Block All these blocks are
Operators
repeated in the same
order in the scripts.
Block name
Variable
My Blocks
text
Now try...
What if you don’t want to position
your sprite in the center each
time? Can you add a parameter
to your “reset” function so that
you can start your sprite in a define reset location
different location each time you
use your function?
121
Three functions
Before you create the program, make This function sets the sprite to a color
effect of 150 and a size of 150%.
these three functions in Scratch. Each
one sets the sprite to be a different
size and color. define big
Events
small define medium
Control
Create the three new
set color ▾ effect to 75
functions in the My Blocks
Sensing
section of the Blocks Palette.
set size to 75 %
Operators
Variable
define small
Abstraction
When you look at something that has too much detail, you
can lose the big idea underneath. In this program, we’ll give
you three different functions. Your job is to take away the
unnecessary details that could be added as parameters later.
Making code simpler like this is called abstraction.
122
The program
The program makes the sprite big, then
First the sprite
small, then medium-sized, and changes grows bigger, then it
its color each time. shrinks to be small.
1
The functions here change the size
and color of the sprite, with pauses in
between each change so you can see
them happen.
when clicked
big
wait 1 seconds
The “wait” blocks
make the code slow At the end the
small
enough for you to sprite grows to be
see the color and medium-sized.
wait 1 seconds
size changes.
medium
You don’t really need three set size to change % Remember you can drag
separate functions to make the “change” parameter
the program work—and here directly from the
functions take time to make. “define” block above.
when clicked
Instead, you can make one
function with parameters! abstracted function 150
wait 1 seconds
Now you can use the
same function to add
abstracted function 50 in the size and color
change effect values
wait 1 seconds you want.
abstracted function 75
123
Remixing
The program draws a cluster
move size
Sound
star cluster
turn 15 degrees
126
when clicked Permissions
This program requires the use of a webcam.
turn video on ▾ Scratch needs your permission to use input from
your webcam. When you click on the Video
set video transparency to 50 Sensing option, a pop-up window will ask you
to allow or block it. You will only be able to try
erase all This clears the this program if you click allow.
stage when the
program starts. x
pen up
Scratch.mit.edu wants to
forever
star cluster
A value of 0 for y means
go to x: pick random -200 to 200 y: 0 the sprite will always
return to the center line
point in direction 90 of the stage.
The sprite appears in the middle of the screen and This sprite jumps around the stage and leaves a
1 only moves when you touch it. 2 star cluster pattern in every place it goes.
127
The second sprite in the program
when clicked
go to x: 100 y: 0
set size to 100 % Change the size of the The sprite will only make a
sprite if you want to. stamp if it senses enough
motion on the webcam.
forever
pen up
128
The final sprite deletes everything Make sure you’ve clicked
when clicked
go to x: 0 y: 0
pen up
go to random position ▾
point in direction 90
Now try...
Don’t forget to remix the
code and add extra sprites
and effects to make the
game more fun!
Move your hands over the first two sprites to make them
3 draw patterns, or touch the third sprite to clear the stage.
129