Posted by & filed under Computer Science, teaching.

This is a follow up from a post I did about a year and a half ago on using Scratch BYOB to teach recursion. The original way of defining parameters in BYOB were a bit clunky, but it’s gotten much better in recent versions so it seems a good time for an update.

A bit about BYOB
If you haven’t tried Scratch in your intro computer science classes run, don’t walk, and give it a try. It’s early October and our intro students just finished their first pass through a unit on list processing. There’s no way we would be where we are if we had to worry much about syntax, and Scratch takes care of that by letting students drag and drop pieces together rather than worry about classes, methods, braces, and semicolons.

Berekely has taken the base Scratch code from MIT and expanded it into a version called Scratch BYOB. Yes, your students will laugh at the name, but it really stands for Build Your Own Block. A block in BYOB is their term for procedure. Personally I switch back and forth between the terms block, function, and method since they’ll be called different things depending on what language we’re using.

And I’m using the 3.1 version of BYOB. 3.0 version had a bit of a difference in how local variables are created, but the concepts are mostly the same.

Creating the Block
First step, we need a block. At the bottom of the variables tab there is a button labeled Make a block. Go ahead and click on that. You should see the following pop up.

The block we’re going to make is the same as the previous posting, a Sierpinski triangle. It’s a bit of a classic for teaching recursion.

You can pick whatever category you want for your block, and it really doesn’t matter which. The category just tells BYOB what tab to put your block on. It will work exactly the same way no matter what category it’s in. Since the drawing commands are under Motion, and we’re drawing a triangle, that’s the category we’ll pick.

For our first block – we’re going to create three – we’ll make it a reporter. In BYOB a reporter is a block that returns a value. Java calls it a return method, Alice calls it a function.

What this block is going to do is take two points and find the midpoint. We’ll need that for later in our drawTriangle block.

Go ahead and press OK. You should see the following screen.

If you mouse just to the right of the blue midPoint label you’ll notice that an orange plus sign pops up on either side. This lets you add labels or parameters.We’re going to add two parameters called pointOne and pointTwo.

You’ll need to click on the little black arrow on to the right of Input Name to drop down more options. We want to set the parameter to be a number and default to 10.

Click OK and repeat for pointTwo. Your block should look like the following.

Now it’s code time. The midPoint block is just going to average the two points so we’ll add them together and divide by two. Scratch BYOB uses a block called report under the Control tab in the same way most languages use return. You’ll also need the addition and division blocks from under Operators.

And we’re done with that block. Go ahead and press OK to save it.

You could have just dropped the operator blocks on top of something at the bottom of the block editor, but I prefer to not have my students use that so their BYOB code is closer to what they’ll do in Python and Java.

The drawLine block
We’ll make the drawLine block in the same way, except this time we’re going to make the block a command instead of a reporter. A command is a block that doesn’t return a value just like a void method in Java.

This method will be a pretty simple block just to draw a point from one point to another. So we’ll need 4 parameters, two Xs and two Ys.

The completed block looks like the following. What we’re doing is 1) pick the pen up just in case it’s down, 2) go to the first point, 3) put the pen down so your sprite will draw, 4) go to the second point to draw the line, and 5) pick the pen up. The wait at the end is just so we can see everything happening when we run our code. If it’s not there BYOB will draw the entire triangle prior to anything showing on screen.

drawTriangle, the last block
Now it’s time to put these pieces together and do a bit of recursion.

Go ahead and make a drawTriangle block in the Motion category. You’ll need 7 parameters, 3 sets of X/Y pairs and a counter so that we can stop the recursion. What I usually do is also include a point reporter that takes two numbers and returns them in a list with x in spot 1 and y in spot 2. But I’m skipping that for here.

Rather than explain the algorithm, I’ll point you to KodeClutz where Google pointed me when I was looking for a quick algorithm.

And the code
Now it’s just a matter of putting our drawTriangle to use. Add the following to the scripts for your sprite.

Run it and you’re set.

A few demos
Below are sample drawings with cnt set to 3, 5, and 7 so you can see how they look. Past 7 didn’t really make much difference visibly. It just took longer to draw.

Sierpinski with cnt set to 3

Sierpinski with cnt set to 5

Sierpinski with cnt set to 7


  1.  Teaching recursion with Scratch BYOB |

Leave a Reply

Your email address will not be published. Required fields are marked *