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! 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:
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.
When the cursor is hovered over the block, a plus symbol represents spots where text or variables can be inserted.
Create 2 variables: number1 and number2. Make sure Input name is checked.
Under Control, grab the if/else block.
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.
Under Control, grab a report _ block for the if statement. Repeat this step for the else statement.
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.
Click OK and grab the finished block under Operators.
Manually enter numbers and run the block.
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.
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.
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.
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.
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.