Giving Your Block an Input Variable

You've seen many blocks that take inputs:
move 10 steps
turn clockwise 15 degrees
go to x:0 y:0
say Hello! for 2 secs
think Hmm... for 2 secs
play note 60 for 0.5 beats
repeat until

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.

  1. Edit your draw square block to give it an input.
    • To edit a block, right-click (or control-click) on it and select "edit."
    • To add an input, click (the last) plus sign in the hat block, type an input name (size), and click "OK."
    • To use the inputed data, drag the input name (size) out of the hat block and place it in your script.
    Use the animation to get the idea. You can read the details (in yellow below) to learn more.
    adding input to block
  1. Check your work. When you click OK (or Apply) in the block editor, you'll see that the draw square block in the scripting area now takes an input. Run this script with a few different inputs to check that it works.
    pen down, draw square (100) Pair Programming Swap
  2. Edit your 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.
  1. Create a draw hexagon block with an input that lets you control its size.
  2. Create a honeycomb block (with an input to let you control the size) that uses your hexagon block three times to create this design:
    three hexagons fitting together around a single vertex
Now Is a Good Time to Save
  Tough Stuff
  1. Create a draw polygon block that takes two inputs:
      draw polygon () sides of length ()
    • a number of sides (call the variable sides)
    • the length of each side (call the variable size or side length)

    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.