High School Computer Science and Programming Workshop
Class 7: Functions


Hesam Samimi, Cliff Kushler
Ananda Living Wisdom School



Here is a direct link to Snap!


To play with Snap! you can just click on the play button on some of the images, like the one above. If you have any problems with it loading, visit Snap! directly in a new browser tab (On that page, click "Run Snap! now"). Another possible solution, if your browser is not Chrome, is to give Chrome a try.


Snap! Blocks

Say I want to calculate how many hours and minutes and seconds are in a duration given in seconds. So I worked hard to get the right set of tiles to do that:

Here is a direct link to this exercise.

Now image I am working on another sprite which needs the same calculation. Wouldn't be nice if I could just reuse my same tiles, rather than having to do it all over again? I suppose I can copy and paste a set of tiles (you can right click and "duplicate" a set of tiles), but still that would take unnecessary space, since I would be just duplicating code everywhere!

Fortunately, Snap! has a solution! It is called a Block. You can create your own kind of tile, called a block, giving it a name, and then putting in it a set of tiles (a script). Every time your new tile is used in a script, it will go and run all the tiles you have inserted in your block.

You can define a new block by clicking on under the Variables section.

Take a look at the program below, where I used a new block which I called "calculate duration" to keep my duration computation. Now in order to use it, I just had to drag my new tile into my script:

Here is a direct link to this exercise.

There you will see that I have defined a block I called calculate duration like this:


Block Inputs

As you see in my block, a block can have one or more Input Variables, (or Inputs, for short) which will be variables that I pass around values to when I am using the block. This will make the block more useful!

Here I have called the input variable seconds. And notice when I am using this new block, I have to provide a value for that input variable. See how I am using it:

So as you see, I am using this block with a value of answer for the input variable seconds. As a result, inside the block, when the tiles are being run, the variable seconds will have whatever value that the variable answer had. Remember, answer is a special variable which stores something the user just typed as an answer to a question.

When you use a block, like I am doing in the above tile, it is called Calling or Invoking the block.

Local Variables

Sometimes it is useful to use a variable only within a block, as part of some computation. After the block is run, the variable is tossed away. It is called a Local Variable, or as it is called in Snap!, Script Variable:


Functions

Snap! blocks represent somethings that in programming are called Functions in general. We create functions to avoid having to redo work. Once a function is defined, we can use it any number of times.

Just like a block, a function can have any number of Input Variables. No matter how many inputs, a function always has none or only one Output, which is a value representing the result of calling the function, which it passes back to whomever called the function.

A function, given the same input values, will always produce the exact same result (and output, if any).

You can think of a function as a kind of a juicer! You can input many types of produce in it and it always produces only one output: a cup of juice. Given the same produce inputs, it will always produce the same juice.


Abstractions

Functions are an example of something that is called an Abstraction in Computer Science. Why? Because when you're creating a function, or a block in Snap!, you are making a new thing that stands for something else...


Next: Class 8: Binary Search Trees
Previous: Class 6: Implementing the Traffic Light Simulation
Back to: Table of Contents