You've seen many blocks that take inputs:
Your draw square
block draws squares of only one particular size—the size built into its script. You can improve your block to draw squares of any size by giving your block an input.
draw square
block to give it an input.
Naming the Input: Choose an input name, like size or side length, that makes the input's meaning clear. Type that name in the input field.
You have two choices about how to treat that new text:
draw square
to draw square picture
, click on "Title text" and type the word "picture.")
Your block header has changed: there is now a variable named size (or whatever name you chose) after the title:
The variable
will contain whatever value the user of your block puts in its input slot. So if the user says draw square (87)
then the value of size will be 87.
To use the size variable, drag the variable where you want to use it. In this case, that's in the move block. If you ever need another copy of a variable, just drag another down.
draw square
block in the scripting area now takes an input. Run this script with a few different inputs to check that it works.draw triangle
, draw house
, and draw row of houses
blocks so that you can draw triangles, houses, and rows of houses of any size you specify as input.draw hexagon
block with an input that lets you control its size.honeycomb
block (with an input to let you control the size) that uses your hexagon block three times to create this design: draw polygon
block that takes two inputs:
Be sure to test out your solution with several polygons with different lengths and numbers of sides.
This draw polygon
block generalizes the pattern of draw square
, draw triangle
, draw hexagon
, and so on. It does so by adding an input that expresses the difference from one block to another, namely, the number of sides. Adding an input to generalize a pattern is going to be an important technique later in the course.
This is an example of abstraction, one of the central ideas of this course and of computer science in general.