Page 4: Making Your Own Block
Unit 1, Lab 2, Page 4
On this page, you will create a new block to make the program feel more like a conversation.
If it isn’t open already, open your U1L2-Gossip project.
Create the block:
Making a New Block
-
Create a new block by either clicking near the end of the palette, clicking the + sign in the top right corner of the palette (shown below), or right-clicking (or control-clicking on a Mac) in an empty spot in the scripting area and choosing “make a block…” (shown below).
- Choose the color, in this case red for Lists.
-
Type the title for your block:
gossip response
. - Select the shape and click OK. For this block, choose the oval reporter shape because you want it to report a value (the chosen response) to another block.
Because you chose the reporter shape, the block editor opens with a block already attached to the hat block. Whatever input you give to report
will become the value reported by your new block.
Coding the Block’s Script
-
Drag in the blocks you need. For your new block, use and . Use the value reported by
list
as an input toitem
. -
Snap the blocks together. The code’s structure will be just like the code inside
who
,does what
, andwho 2
. -
Fill all the inputs.
-
For this project, click the down-arrow in
item
to change “1” to “random”. -
Then fill
list
with phrases like “Guess what,” “I heard,” or “Who cares!”
-
For this project, click the down-arrow in
- Click OK when you’re satisfied.
: Procedures, Reporters, and Commands
A procedure is a named sequence of instructions that may take inputs and may report a value. Some languages call procedures methods or functions. Here are two types of procedures you have seen in Snap!:
Reporters have an oval shape. They report a value.
Commands have a jigsaw puzzle shape. They tell the computer to do something without reporting a value.
The procedure would be written as
PROCEDURE double(x) { RETURN(2 * x) }
or .
Many languages (and the AP CS Principles Exam) use
return
instead ofreport
as the name of the command to give a value back at the end of a function call.The exam uses “value of a procedure” to mean the value the procedure returns when called. For example, “the value of
double(5)
” means 10.
Debugging
- Test your custom
gossip response
block by clicking it several times. It should behave like thewho
,does what
, andwho 2
blocks. Debug any problems before moving on.
Review how to edit a block in Unit 1 Lab 2 Page 2: Customizing and Debugging, if needed.
- Use your new block: Edit
gossip 2
, and insertgossip response
in place of the text “Oh, but….”
Why do I see plus signs around gossip 2
in Snap! but they’re not in this picture?
Normally, when you are using the Block Editor, you see plus signs because you have to click on them to add inputs to your blocks. But to show you the finished code here, it looks cleaner without the plus signs.
Test and debug
gossip 2
also.Then test the whole program by clicking script D several times, and fix any bugs.