ST EK List:
2.2.1 Develop an abstraction when writing a program or creating other computational artifacts. [P2]
2.2.1A The process of developing an abstraction involves removing detail and generalizing functionality.
2.2.1B An abstraction extracts common features from specific examples in order to generalize concepts.
2.2.1C An abstraction generalizes functionality with input parameters that allow software reuse.
5.3.1D Procedures have names and may have parameters and return values.
5.4.1G Examples of intended behavior on specific inputs help people understand what a program is supposed to do.
MF: 1.2.4 Collaborate in the creation of computational artifacts. [P6]
5.1.2D Program documentation helps programmers develop and maintain correct programs to efficiently solve problems. (with documentation)
5.1.2E Documentation about program components, such as segments and procedures, helps in developing and maintaining programs. (with documentation)
5.1.2F Documentation helps in developing and maintaining programs when working individually or in collaborative programming environments. (with documentation)

Blocks with Inputs

Ruthless Suggestions from Al

Branch Cycle Animation

On this page, you will create many different pictures (like the ones above) by creating a single block with an input slot: pinwheel, branches:(). Input slots allow one block to do many related jobs.

  1. Click here to load this file. Then save it to your Snap! account.
    Make 5 copies of the script and modify those copies so that they make pinwheel designs like these with 3, 4, 5, 6, and 12 branches.
    pinwheel with 3 branches pinwheel with 4 branches pinwheel with 5 branches pinwheel with 6 branches pinwheel with 12 branches

The scripts are essentially all the same; only the input values change. Instead of having many separate scripts, you can make one more general script that will draw all such designs. Your pinwheel block will need an input to specify how many branches you want, pinwheel, branches:() just as the go to block has inputs to specify x and y values go to x:() y:().

Pair Programming Swap

Debugging Tip: Commenting Your Code

From Brian:

Oh, about the orange box on 1.3.3, I think the comment on the hat block is okay, but the comment on the turn is exactly the sort of thing I wouldn't do. In fact I might make a point of saying that when you're /teaching/ someone, you (we) put in comments that you wouldn't use if you were just programming for yourself.

But the hat block comment in the movie isn't very good documentation. It should say "Draws a REGULAR five-sided polygon, with SIDE LENGTH set by the input." (Not really capitalized; I just did that to emphasize to you what's missing.)

Really it's hard to give an example of useful commenting that doesn't look stupid unless you have a big project. Then, for example, when some global variable is used without being set, you might want to attach a comment "FOO is set in procedure BAR that's called in the initialization."

Adding comments to your code helps you remember what your code does, helps other people reading your code to understand it, and can help you avoid bugs by increasing the clarity of your program. This animation shows how to add comments to your Snap! code by right-clicking (or control-clicking) in the Scripts area and choosing "add comment."

Notice that a comment attached to the hat block in the Block Editor becomes the help message for a custom block.

Animation on how to  add a comment to a block and how a comment added to a hat block becomes that custom block's help message.
  1. Create a pinwheel block with an input for the number of branches. Instructions are below and you can also watch the process in the video that follows the instructions.

    Making an Block with an Input

    1. Type the block name and the label for your variable in the "Make a block" dialog box, and click OK. For this project, type pinwheel, branches:
      Make a block
      • This block is about moving your sprite, so you might choose the blue Motion palette. Or just leave it gray.
      • Using a comma and a colon are not requirements; they are here to help with clarity.
    2. Click the + (plus sign) after the variable label, type the name of the input, and click OK to add the variable input slot. For pinwheel, click the + after "branches:" and type number of branches as the name of your input variable.
      Plus sign Creat input name
    3. Drag in the blocks you need, and use the new input.
      • Here, drag one of your pinwheel scripts into the Block Editor and snap it to the pinwheel block.
      • We use the word "input" for two different but related things:

        1. The input name, such as number of branches, which is set in the block definition. This is called a parameter (or formal parameter).
        2. The input value, such as 6 for a hexagonal pinwheel, which is given each time the block is used. This is called an argument (or actual argument).

        The AP CSP framework uses the word "parameter" for both.

      • Then, drag the new input number of branches in to replace the values that change to allow for more general use of the script.
      pinwheel, branches:(number of branches){repeat(21){move(100) steps; wait(0.1) secs; move(-70) steps; wait(0.1) secs; turn clockwise(360/21) degrees}} pinwheel, branches:(number of branches){repeat(number of branches){move(100) steps; wait(0.1) secs; move(-70) steps; wait(0.1) secs; turn clockwise(360/number of branches) degrees}}
    You can watch this process in the video below.
    Animation summarizing how to create a block with input.
  2. Comment your code as shown above in the Debugging Tip.
  3. Save Your WorkSave your work as U1L3-Pinwheel.
  1. These images were created with the pinwheel block and set pen color. Create your own art.
    wreath of rainbow colored pinwheels wreath of rainbow colored pinwheels wreath of rainbow colored pinwheels
  2. Make the animation at the top of this page.