Programming Styles

In this activity, you'll learn about the difference between programming by nesting reporter blocks inside each other and programming by stacking command blocks top to bottom.

Commands Versus Reporters

Before this unit, you nested blocks inside each other so that the output reported by one block became the input to another. That style of programming is known as functional programming, and in Snap!, it uses reporter blocks. A reporter computes a value and reports it (to you or to other blocks).
songify (emojify (uppercase ('They love soccer so much that it's funny'))) reporting '🎶 THEY ❤️ ⚽️ SO MUCH THAT IT'S 😂  🎶'

In this unit, you've been stacking blocks that carry out actions, such as playing a note. This style of programming is called imperative programming. In Snap!, imperative programming uses command blocks. A command tells the computer to do something, (play a sound, draw, move, change something, etc.). Commands have effects.
repeat (2) {
	play ♪ pitch: (64) beats: (0.5)
	play ♪ pitch: (63) beats: (0.5)
}
play ♪ pitch: (64) beats: (0.5)
play ♪ pitch: (59) beats: (0.5)

The shapes of the blocks go with the shapes of the places where you can use them. Reporters are round, and they go into round slots in a block. Commands are more rectangular, and they have a tab at the bottom that fits into the gap at the top of another command block. Snap! won't let you put a reporter in a stack of commands or put a command in a round slot.

You learned about reporters in Unit 1 Lab 1 Activity 4: Say Hello to Snap!.

A reporter block does a calculation and reports the result. Reporters have an oval shape.
item () of 'list input slot' join () () if 'predicate input slot' then () else ()

You learned about predicates in Unit 1 Lab 6 Activity 2: L33t Text.

A predicate is a special kind of reporter that asks a true/false question and reports either true or false. Predicates have a hexagon shape.
What does IS () A (LIST)? do? mouse down? () = ()

You'll use commands much more in Unit 2.

A command block tells the computer to do something without reporting a value. Commands have a puzzle shape and can be snapped together one after another.
set tempo to () bpm repeat () {} delete () from 'list input slot'

Unlike reporters, a command block doesn't report anything, so you can't use its result as the input to another block—its result is an action, not a value!

Creating a Command Block

You've created a reporter block before. You can create a command block in almost exactly the same way.

Note to Mary for later: Check this link next year after the 1.2 page has been split up. --MF, 2/5/22
You learned how to create a reporter in Unit 1 Lab 2 Activity 2: Refining the Story.

    Note to Mary for later: Older instantiations of this image in Sparks 1.2.2 and CSP 1.2.4 need to be updated for the new palette list ordering. Also the "text box label 'Type a title for your block'" and "label 'Select a shape'" are both gone now. And "with no label of which 'for all sprites' is selected" could be deleted. --MF, 2/9/22

    No, it's this picture that should change. The "new ordering" was a bug, now fixed. Also, should this picture be made at zoom level 1 instead of 1.5 so that it looks the way students will see it (with the category chooser centered etc.)? --bh 3/3/22

    image of 'Make a block' dialog box with 10 palette menus (Motion, Looks, Sound, Pen, Other, Control, Sensing, Operators, Variables, Lists) of which Sound is selected; a text box in which 'mystery song' has been written; three block shape options (puzzle-shaped/'Command', oval/'Reporter', and hexagonal/'Predicate') of which 'Command' is selected; two radio boxes ('for all sprites', which is checked, and 'for this sprite only', which is not checked); and two buttons ('OK' and 'Cancel')
    I wonder if we should have them save this as a separate project called "Mystery Song". --MF, 2/27/22
    I don't feel strongly either way, or at least not so far. Not sure if I'll change my mind when I go through rest of lab. -PF 3/3/22
  1. If your project isn't open already, log in to Snap!, and open your "Song Player" project.
  2. We should have a pic of mystery song somewhere around here. --MF, 4/11/22
  3. Create a new command block called mystery song.
    1. Right-click in an empty spot in the Scripts area and choose "make a block..."
      make a block option
    2. Choose the palette for your block: Sound.
    3. This should be "choose a palette" instead of "choose a color," right Brian? Need to change in Sparks 1.2.2 and CSP 1.2.4. --MF, 2/9/22

      I'm confused by this red box comment. Each attribute of a block has a meaning and a visual representation. So:
      color : palette :: shape : report behavior
      You seem happy to use the representation as the name for Shape, so why not be consistent and use the representation for Color too? Or else change both of them (after inventing a good name for the meaning of the shape; mentally I call it "range" as in "domain and," but that's a stretch. -bh 2/15/22

      P.S. Apparently all this time Jens (following Scratch usage) uses "Palette" to mean that whole left column of the screen, all colors, and uses "Category" to mean what I've been calling the palette. I don't know why he didn't tell me that right at the beginning! My feeling is that changing our vocabulary is too horrible to contemplate. Oh well.

      BRIAN, would you be happy with "Category" both in the pic and in the text? For example: "Choose the category for your block: Sound. ... Select the shape for your block: Command." I understand your point about correlation, but something someone once told me about hobgoblins comes to mind. ;) I think "category" and "shape" will be more helpful to students than "Choose the color for your block: Sound." or "Select the report behavior [or "range"] for your block: Command." even if the two I suggest are a bit mis-matched. Thanks for reviewing this! --MF, 2/18/22

      I'm happy as it is! I just commented because you asked. I think we probably shouldn't change it to "category" here unless we're going to do that systematically through the curriculum, both curricula actually, and that's probably not on. -bh 2/21/22

    4. Type the title (name) for your block: mystery song.
    5. Select the shape for your block: Command.
    6. Click "OK."

By the way, I always try to get people to distinguish between a command, which is a block you find in the palette, and an instruction, which is a command with its input slots filled in. This is parallel to the distinction between a reporter and an expression. I'm not suggesting we teach kids to obsess over this, but I'd be happier if we just modeled that use of language so students who are reading closely might pick up on it. --bh 3/3/22

Are you making a suggestion for this text here or a general remark. Please feel free to remove this comment and address as you see fit. --MF, 3/6/22

When you create a reporter, the block editor opens with report attached to the hat block, and whatever input you give to report will be the value reported by the new reporter. When you create a command, the block editor opens with only the hat block, and the blocks attached below it will run (one at a time) when the new command is run.

    : Abstraction

    Hiding away the details of how code works (such as by moving it inside a new block) is a form of abstraction.

  1. Inside the mystery song block, either build a new song for someone else to guess or drag in a song you have already created and attach it to the mystery song hat block.
  2. Note to Mary for later: An animation of dragging in a song would be nice here. --MF, 2/9/22
  3. Play your mystery song for someone else, and see if they can guess what it is. Then guess someone else's song.
    You can find your newly created block at the end of the Sound blocks in the sidebar, and drag it out to the scripting area.
  4. Now Is a Good Time to Save

  1. Create additional command blocks with different mystery songs.
In this activity, you created a command block to store a sequence of commands.