## Sunday, December 7, 2014

### The function game (programming Class 12)

who: Baan Pathomtham Grade 5 class
where: at school
when: Monday morning (bright and early!)

# Functions

As with loops, we are spending time focused on functions to make sure that the students master these concepts.  Today, we started with a function game. I drew a function machine on the board and gave it a name. Each student took turns giving me the value of an input, then I would tell them the output. Their objective was to figure out the function rule. Early on, I added a table showing our input and output values, to keep all the information organized.

The picture shows our starting function: constant 5.  I was pleased that, even on this simple function, we got to see an example where an invalid input was tried, so we could talk briefly about the domain of definition.

Other functions we tested were LIN(x) = 20 - x and Pyth(z) = z^2 + 1.  The students were pretty fast about guessing all of these.

Next, Boongie and Win got to lead the game.  This was a lot of fun, though calculation time slowed us down a little bit, especially when students input large numbers.  I'd note that there was little systematic thinking about what inputs to use, but this was the first time we've played.

Next time we play, I will use a non-numerical function to remind them that functions aren't only about arithmetic.

# Homework review

Homework links: TitusBoongieWin, and Kan (not done!)

Our usual opportunity to talk about the code that the kids wrote.  There wasn't much to report here, though Win got a bit stuck (he didn't finish his code) and Kan didn't write the program. We did talk a bit more about Kan's boxbox program which nicely illustrated three things about pencilcode functions:
1. They can have multiple inputs
2. Numbers, words, arrays can all be inputs (indeed, the possibilities are much broader than this)
3. Inputs can be left out and the program will still run.
Of course, we already knew most of these things, but it is nice to have rediscovered them by accident and it makes a break from the number-centric functions that were used in our number game.

There's a link above to the program, this is what it produces (press the button to run):

I showed them a couple of programs I wrote.  This one piggy-backs off their use of switch-when-then-else to build a long list of people and birthdays: long list. This other one was an attempt to add some additional functionality and include an example of composition of functions: bells-and-whistles.

# spike and starburst sequence

The main challenges of the day were to replicate these animations: SpikeSB0SB1

Spike
We were lucky that Kan chose to write all the steps explicitly, while Titus used a for loop. This allowed us to compare and contrast and we saw that the for loop wins out because:
• it takes fewer lines of code (9 vs 27)
• it is easier to debug (consequence of the fewer lines of code)
• it can be changed easily. For example, sending all the spikes out 10x farther required 6 key strokes, while the other program required a lot of changes (nearly every line of code)
Starbursts
These were a bit of a challenge and there is still more work to do for most of the students. Next time, we will continue with these and move on to the third starburst version (passing a function as an argument to another function.)

# Homework

The homework this week is very challenging: to create an animated function machine. The program should draw a picture of their machine, ask the user for an input into the function, use animation to show that input going into the function and animation to show the output coming out of the function machine.

For user input, they can use the requester( ) function and program phrase from this code: http://jgplay.pencilcode.net/edit/class/jgBirthdaySwitch.