Skip to content
Advertisements

Snap! (BYOB): Lesson 4: Building a Number Comparison Block Using a Reporter and How It Translates to C++

For background information and how to get started with Snap! (formerly known as BYOB), click here for lesson 1. I’m using BYOB version 3.1.1 for my examples, but you can follow along with either version linked below.

Snap! homepage

BYOB download

Previous lessons:

Snap! (BYOB): The Drag-and-Drop Programming Language for Beginners: Lesson 1

Snap! (BYOB): Lesson 2 – Making a Custom Block and Introduction to Costumes

Snap! (BYOB): Lesson 3 – Building a Number Guessing Game Using Variables, Loops, and If/Else Statements

Snap! is a great learning tool for programmers of all ages. It helped me get into a programming mindset as a Computer Science major and also gave me a fun way to introduce programming to my 12 year old. The following program is easily built in Snap! but also touches on concepts in C++, such as console output, returns, and functions.

This lesson introduces the reporter block through a simple comparison program and shows the relation to the C++ language.

For the first exercise, I’m using blocks from the following categories:

  • Control
  • Operators
  • Variables

Under Variables, click the “make a block” button. First, select Operators as the block’s category because it’s math related. Second, change the option from command to reporter. The finished program will report back the answer instead of making our Sprite say the answer on stage. Third, name the block. “Smaller number between and” may seem like gibberish, but variables will fill in the gaps. Click OK.

Screenshot (99)

Screenshot (100)

Inside the new block

When the cursor is hovered over the block, a plus symbol represents spots where text or variables can be inserted.

Screenshot (101)

Create 2 variables: number1 and number2. Make sure Input name is checked.

Screenshot (102)

Screenshot (103)

Block after adding both variables

Under Control, grab the if/else block.

Screenshot (104)

Under Operators, grab the _ < _ (comparison) block. Once the comparison block is inserted right of the “if,” drag number1 and number2 into that block. Remember: when you want to duplicate a variable, simply click and drag the first one. If you click and drag another variable, you will actually move the variable instead of making duplicates.

Screenshot (106)

Under Control, grab a report _ block for the if statement. Repeat this step for the else statement.

Screenshot (107)

The logic is simple: if number1 is smaller than number2, report back number1. Else report back number2. Drag the variables into their correct spots to finish the program.

Screenshot (108)

Click OK and grab the finished block under Operators.

Screenshot (109)

Manually enter numbers and run the block.

Screenshot (110)

It works!

One of the most powerful and useful tools in C++ are functions. Read more about functions on cplusplus.com. They can get rather complicated, but I’ll keep things simple here. Think of it as a way to reuse code. For example, you can write a function that sorts a text file. You can call that function every time you need to sort a file instead of writing that longer chunk of code over and over.

How this relates to the block we built in Snap!

In Snap!, we have a say block that makes the Sprite say something. He doesn’t do anything with the information; he just says it. The say block is like the standard output stream (cout) in C++.

In Snap!, we have the report block. It gives us back a value instead of just saying it. That allows us to take that value and do more operations on it.

The following code is written in C++ using QT Creator. It’s basically the same block we built, using the Snap! equivalent of the say block instead of the report block.

My function is getSmallest with parameters number1 and number2. It compares the numbers using an if/else statement and prints out the answer. Down in main, both variable values are declared and the getSmallest function is called. firstNumber and secondNumber are passed to the function and it goes to work.

Screenshot (126)

Use void when you do not need a value returned

Screenshot (127).png

Result of getSmallest function

What if you wanted to get a value back from the function and do more operations on it before printing something out? That’s where the report block in Snap! comes in, or return, in C++.

Code changes: void changed to int and cout changed to return.

Screenshot (128)

Declare type when you want something returned

This version compiles but it actually prints nothing. When the values are passed from main, the function no longer prints anything. So main needs adjusted.

Screenshot (130)

This works but is pointless without further operations

The following code does something with the returned value. answer is declared as the returned value of the function. Then, finalAnswer equals the answer plus 100. The last line prints out the final answer.

Screenshot (132)

Now there’s a reason we returned that value

Screenshot (133)

Snap! equivalent:

Screenshot (135)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: