## Saturday, November 29, 2014

### Flowers, Squares, and Functions (programming class 11)

when: Monday morning
where: at school

Homework review
Forests/flower gardens (some of these were old?):

The first led very nicely into functions, the topic of the day as he had put the petal drawing into a function.  The second was a really nice cap to our discussions about for loops. In his original version, it drew one lime flower and three blue ones. Working together, the class modified the code so that all the flowers were a different color.

Win showed us his flower program, cryptically named untitled5: http://win.pencilcode.net/edit/untitled5

This is a good candidate for streamlining using a function.  Something for him to consider over the week.

To round out the discussion, I showed two related programs that I had found from other users and modified: http://jgplay.pencilcode.net/edit/otherPpl/flowerPattern

We didn't discuss this program as Titus thought it had gotten lost: http://titusorc.pencilcode.net/edit/flower/flowers

Focus for the day: Functions and vectors
Today, our plan was to build up a more complex function structure based around drawing some simple squares.

The first task was just to create a function that would draw a single red square. This code is an example.

Once completed, we added an input variable that would allow them to control the size of the square. Finally, we wanted them to make nested squares that would show why it is useful to capture a block of generic code in a function.  Here's our example.

At this point, Kan had two interesting ideas: First, he wanted to add the color as a parameter to the function.  That was pretty easy, but it was a nice way to see that functions can take numbers and words (in this case a color keyword) as arguments and that they can take multiple arguments.  His next idea was to include a variable that would tell the turtle to move after drawing the square. I've copied his code here

http://jgplay.pencilcode.net/edit/class/kanBoxboxboxbox1234

He wasn't quite sure how to properly call the function.  Notice what he did: three arguments to the function, one of which is a vector!  Good stuff . . . and we will discuss this more next time.

We also talked about the idea of functions in mathematics and worked through three examples:

• f(x) = x+2
• goal(y) = 2y
• birthday(name) = {the day of that person's birthday}

Homework
The challenge this week is to write the birthday function that will work whenever a member of their immediate family is entered as the input variable.  This is not going to be easy, so we expect them to experiment, explore, and ask questions if they get stuck.

Some other interesting code
In preparation for the class today, I noticed Kan had created this program:
http://jgplay.pencilcode.net/edit/otherPpl/kanthapStinky

Both Win and Kan had these programs

At some point, we will return to these programs to talk about what they do and why.  I know the two were experimenting with them at the end of class and I'm eager to see what they can discover.