Scratch Advance

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Functions

What do you do when you want to run the same bit of


code several times but you want to do something else
in between? Then it’s time for a function! Functions let
you name a special section of code that you want to
use later. Then, when you want that code in a program,
you can just use your named block!

Make a function block To complete your function, add


As with a variable, you have to create a function block the code that you want below
the “define” block. You can then
before you can use it in a program. The code for a function use the block in your program.
has two parts: the definition block and the name block. You
can make a function in the “My Blocks” section. Here, we’ll
create a function that makes a sprite draw a triangle. define draw a triangle

You can find the


Code Costumes Sounds
pen tools in Add pen down
Extension.
My Blocks
Motion
Make a Block
repeat 3
Looks Give your function a
draw a triangle
move 70 steps
Click on Make name that describes it so
Sound a Block and a that you can remember
turn 120 degrees
pop-up window what it does when you
Events will appear. want to use it later.

Control Make a Block


pen up
Sensing
Block name
Operators

when clicked
Variable
text
draw a triangle
My Blocks Add an input Add an input Add a label
number or text boolean

Pen Run without screen refresh

Cancel OK

When you have named


your block, click 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.

set pen color to

draw a triangle Add your function to


draw a triangle shape.
turn 30 degrees
There are 360 degrees in a
circle. As the loop repeats
12 times, you want each
Now try...
go to x: 80 y: 0 triangle to be drawn a farther What other drawings
30 degrees around, as there are can you make with your
repeat 8 12 times 30 degrees in 360.
triangle? What happens
if you change the number
set pen color to
of repeats and how many
draw a triangle
degrees the triangle turns
in the program?
turn 45 degrees

go to x: -200 y: -200

This block will move the sprite


out of the way so that you can
see your finished creation!

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

To add the “size”


Code Costumes Sounds pen down block to the “move
steps” block, drag a
My Blocks repeat 4 copy of it from the
Motion
“size” part of the
Make a Block size
move steps above function.
Looks
Make a Block
turn 90 degrees
Sound

Events draw a square size

pen up

text

Add an input Add an input Add a label


number or text boolean
draw a square 50
Run without screen refresh

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 55 This function with


parameter block
turn 36 degrees draws a square with
sides 55 pixels long.

The sprite turns 36 degrees This moves the sprite


for every repeat. Since there go to x: 0 y: 0 to a new position to
are 360 degrees in a circle, the start drawing a second
loop needs to repeat 10 times. repeat 9 circle of squares.

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?

Ask user Spell This part of the algorithm


for their the word will require some more
Start
favorite complicated code.
backward
animal

This can be done


in Scratch using
Ask user Spell
a single block.
for their the word
favorite End
backward
food

This step will only This part of the algorithm


need one block in appears twice, so it might
your code. be useful to make a
function to do it.

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

This step can be


broken down into Spell The sprite will need
smaller steps. How to say the same
the word Sprite repeats
many smaller steps number of letters
will you need?
backward letters for that as in the answer.
number of times

The sprite needs to


say each letter.
Ask user Sprite says the
for their letter at the
favorite variable’s position
food

After one letter is


said, the sprite needs
Subtract 1 from to say the previous
Spell the variable letter in the word.
the word
backward
This step does
the same thing as
Once the function
before. Can you
Return has run, the code
make a function
should return to
with parameters to main
the program.
that works for both? program

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

name everything in a way that reminds Make a Block


Sound
you what your blocks are used for.
Events spell backward word

Code Costumes Sounds


text
Variables Add an input Add an input Add a label
Motion number or text boolean
Make a Variable
Run without screen refresh
Looks my variable
Cancel OK
Sound number of letters

Don’t forget to add a parameter to


Events
your function so the user can add
This variable will keep a word into the code.
track of the letters in
each answer.

Defining your function


Now you need to tell your function what to do
when it’s time to spell a word backward. Try to
match the decomposed steps to each block of code.
In order to place the “word” block
into the code below, drag it across
define spell backward word from the “define” block.

set number of letters ▾ to length of word

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

say letter number of letters of word for 0.5 seconds

change number of letters ▾ by -1 Every time the code says a


letter, it takes 1 away from the
The “repeat” loop will make the variable, so on the next repeat
program keep saying letters until the it says the letter before.
variable gets to 0. This happens when
the whole word has been spelled out.

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

spell backward answer


Your variable value will
show here if the box
beside the variable in the
Blocks Palette is checked.

numLetters 0 numLetters 5

What’s your favorite animal? Backward, that’s spelled:

This box appears


automatically when you
use an “ask” block.

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.

numLetters 3 Watch the variable numLetters 0


value count down
each time the
r What’s your favorite food?
loop runs.

The sprite will now


The sprite will spell your favorite
say each letter food backward.
one by one.

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 %

when space ▾ key pressed clear graphic effects

go to x: 0 y: 0 wait 2 seconds

point in direction 90 forever

switch costume to costume1 ▾ go to random position ▾

set size to 100 % if touching edge ▾ then

clear graphic effects change size by -20


wait 2 seconds turn 15 degrees

change color ▾ effect by 25

when this sprite clicked else


go to x: 0 y: 0 change size by 20

point in direction 90 turn 37 degrees

switch costume to costume1 ▾

set size to 100 % next costume

clear graphic effects wait 0.5 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

Add an input Add an input Add a label


number or text boolean

Run without screen refresh


reset
Cancel OK

All three scripts can


Press OK when now start with the
you’re done. new function.

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

set color ▾ effect to 150


Code Costumes Sounds
set size to 150 %
My Blocks
Motion
Make a Block
Looks
big This function sets the sprite to a color
Sound effect of 75 and a size of 75%.
medium

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

This function sets the sprite to a color


My Blocks effect of 50 and a size of 50%.

define small

The sprite starts as one


color and size, which the set color ▾ effect to 50
functions change.
set size to 50 %

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

define abstracted function change

2 set color ▾ effect to change

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

1 of three stars when one sprite


is touched. Two functions are
needed to create this effect.

Make sure you’ve clicked


When you remix a project, you on Sprite 1 before
creating this code.
take something that’s already Sprite 1

been created and change it or The first function draws a star


shape. It has a parameter so you
add to it so that it works better can draw stars of different sizes.

for you. Remixing can be a form define draw a star size


of collaboration or it can be a way
to add variety to a project that pen down

you have made yourself. repeat 5

move size

turn 144 degrees


The program
Here is a program that lets you move sprites
around and create patterns using a webcam.
Can you use the skills you’ve learned in this pen up
book to remix the program and make it even
better? This function uses the first to draw three
stars next to each other, all of different sizes.
Try changing the values to remix the pattern.
Code Costumes Sounds

My Blocks define star cluster


Motion
Make a Block
Looks draw a star 30
draw a star size

Sound
star cluster
turn 15 degrees

Events move 30 steps


The program uses
Control two functions, one draw a star 50
with parameters.
Sensing turn 80 degrees

Operators move 40 steps

Variable draw a star 15

My Blocks turn 65 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

Use your camera


go to x: -100 y: 0
Block Allow
set size to 40 %

forever

if video motion ▾ on sprite ▾ > 20 then

pen up If the sprite senses motion


on the webcam, it will jump
to a random position and
go to random position ▾ draw a star cluster.

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

2 makes a stamp of itself in a different


size each time you hit it. You could Sprite 2

remix it to make more or fewer stamps,


or to change its color every time. Make sure you’ve clicked
on Sprite 2 before
creating this code.

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

if video motion ▾ on sprite ▾ > 20 then

pen up

go to random position ▾ Make sure to pick a


sprite with more
than one costume.
next costume

set size to pick random 10 to 200 %

set ghost ▾ effect to 50 The “ghost” effect


will make the sprite
a little see-through
stamp This will leave before it stamps.
a copy of the
sprite’s image
set ghost ▾ effect to 0 wherever you
use it.
set size to 100 %

go to x: pick random -200 to 200 y: 0

After making a stamp, the


sprite will return to the
center line of the screen. i
Useful tip
Whenever you create a program that makes a
sprite change costume, make sure you’ve picked
a sprite that has more than one costume already.
Alternatively, you can make extra costumes
for the sprite yourself.

128
The final sprite deletes everything Make sure you’ve clicked

3 on the screen when you collide


with it. This one will clear the Sprite 3
on Sprite 3 before
creating this code.
screen when it gets too messy.

when clicked

go to x: 0 y: 0

set size to 70 % A “forever” block means the following


code will run anytime you collide with
forever the sprite, not just the first time.

if video motion ▾ on sprite ▾ > 20 then

pen up

go to random position ▾

This sprite will delete all the star clusters


erase all and stamps on the screen when it is touched.

go to x: pick random -200 to 200 y: 0

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy