Posted by & filed under Computer Science.

One of the biggest limits we came across teaching with Scratch this year is the inability to write methods and use parameters.  Methods, returns, and parameters are probably the only major concept in our intro CompSci class that we can’t teach in Scratch; so we introduced them in Alice this past year.  It worked ok, but we haven’t been able to come up with any really good labs that explain the purpose of parameters well to the students.

This past week I came across an extended version of Scratch called Scratch BYOB, or Build Your Own Block.  And it does exactly what I was looking for.  It gives you the ability to create methods and optionally include parameters.

I’ve updated this post to do the same using the 3.1 version of BYOB which is current as of Oct ’11. You’re probably better off with that post instead unless you’re using an earlier version of BYOB.

I’m assuming you’ve at least taken a look at Scratch.  If you haven’t, head on over to the Scratch website.  There’s a ton of good stuff there.

A Quick intro
For my first attempt with this I’m going to do the classic Sierpinski triangle.  Seems that’s pretty much the standard recursion problem so it’s a good place to start.

Creating the method
The first thing to do is click on the variables section.  You’ll notice an additional option titled “Make a block.”

Clicking on the Make a block option brings up a dialog box where you specify both the name and any parameters.  This is where I found the only negative that I’ve come across so far with BYOB.  If you have a long block specification it goes off the right edge of the text box and does not scroll.  You’ll notice in the image below that I used several parameters and ran out of room.  In fact there are 7, although you can only see 6.

A Reporter and Boolean are both return methods.  A Reporter returns a string or number and the boolean returns, well, a boolean.

Our first method
The first method to create is one that draws a line.  Sure, we could put all this in one method but that sort of defeats the purpose of using a method, right?

So with a little dragging from the block list into our method we have the following drawLine method which will get used to draw triangles in our next method.

Nothing too complicated here.  Pen is picked up, the sprite moves to the first point, the pen is dropped, sprite moves to the second point, and the pen is picked up just in case.  The wait was put there originally so you could see what’s going on.  But I found that if the wait wasn’t there Scratch would appear to freeze while the triangles draw and then the entire design would show up.  With a very short wait you can see each line drawn.

drawTri method
Our triangle drawing method needs 7 parameters.  6 to hold the 3 x,y pairs we need and 1 to keep track of the recursion level.  Were we doing this in another language we could pass in the points as a list or maybe even make a class to hold the points.  But passing in 6 numbers works well enough to teach the concept.

After a little more dragging and dropping we wind up with the following.  Thanks to KodeClutz and Google for the algorithm.

A little breakdown…  The first 3 lines draw the outer triangle going from (x1,y1) to (x2,y2), (x2,y2) to (x3,y3) and back from (x3,y3) to (x1,y1).  The next 6 set local variables to the x and y for the 3 midpoints.  And the last few lines recursively call our method to draw the smaller triangles if count is greater than 0.

This also brings up the idea of local variables and will be a good place to introduce the concept of scope, something else that we have struggled to find a good way to get clicking in the students.

Starting it up
We need to call our drawTri method once to get the ball rolling.  I also wanted to pick up the pen, clear the screen, and hide the sprite.

And a few samples…
Below are sample drawings with count set to 3, 5, and 7 so you can see how they look.  Much past 7 didn’t really look all that much different.  It just took longer to draw.




Does it work?
Honestly, don’t know yet.  We’re done with Scratch for the year so we won’t know until next year.  Even if we don’t get to recursion in Scratch though, the ability to introduce methods in this environment should help our kids pick up the concept earlier in the year and without as much difficulty.

5 Responses to “Teaching recursion with Scratch BYOB”

  1. Randy

    I had trouble getting your code to work, but it works now. THe problem I have is with the ‘local’ block variables. Mine aren’t working as local variables (midx1, midy1, etc), apparently they’re having a global effect, which throws off the drawing.
    I just substituted the expression for each into the recursive call, skipped using the local variable names. THis works now.
    How do you create local variables? My version of byob (windows) doesn’t have the ‘block variable’ creator?

  2. Ryan

    This is actually from an older version of BYOB. Don’t remember what version it was we used in class, but the process of making parameters was much easier than what’s here.

    Can’t seem to get to the BYOB site right now to try out whatever the current version is.

  3. Randy

    Thanks for checking, I’ll try to find a place to ask at the byob site.
    They have something called a script variable…but this test doesn’t seem to work the way I think it should –
    make a regular variable x, set it to 0.
    make a block Test with a script variable x, set it to 10.
    I think both variables are changed to 10, rather than just the variable local to Test block.
    Let me know if you find out something.
    Thanks for your Sierpinski program. We’re teaching Scratch and maybe byob to elementary school kids in a school in Fairfax county Va.

  4. Ryan

    Checked this morning. The version we used in class this year is 3.03 which has a small box out to the side of the block editor to create local variables. I downloaded the new version of BYOB last night, but didn’t get a chance to try the script variable idea.



  1.  Teaching recursion with Scratch BYOB – take 2 |

Leave a Reply

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