Students encounter new image transformation functions and strengthen their understanding of Circles of Evaluation by using functions within other functions.

 Prerequisites Lesson Goals Students will be able to: Demonstrate understanding of the Order of Operations Use Circles of Evaluation to combine multiple functions, including non-Number producing functions Student-facing Goals I can use Circles of Evaluation to combine many kinds of functions. Materials Supplemental Resources Preparation Make sure all materials have been gathered Decide how students will be grouped in pairs Key Points For The Facilitator Check frequently for understanding of data types and contracts during this lesson and throughout subsequent lessons. When students encounter errors, encourage them to check their Contracts page and show their work using Circles of Evaluation. Students will use their Contracts page frequently, so it should be kept in an accessible, convenient location.

Glossary
circle of evaluation

a diagram of the structure of an expression (arithmetic or code)

contract

a statement of the name, domain, and range of a function

datatypes

a way of classifying values, such as: Number, String, Image, Boolean, or any user-defined data structure

definitions area

the left-most text box in the Editor where definitions for values and functions are written

function

a mathematical object that consumes inputs and produces an output

Image

a type of data for pictures

interactions area

the right-most text box in the Editor, where expressions are entered to evaluate

## 🔗Composing Functions 20 minutes

### Overview

Students are given a scaffolded activity that forces them to use the output of one function as the input to another - to compose them. The Circles of Evaluation are extended to provide a visual-spatial metaphor for function composition, in addition to Order of Operations.

### Launch

Students should be logged into code.pyret.org and have their workbooks with a pen or pencil.

Divide students into groups of 3-4, and distribute a set of function cards to each group. Write down pairs of integers on the board, representing the "starting numbers" and "ending numbers". These integers should range from -50 to +50, but you can change the difficulty of the activity by making that span wider (more difficult) or more narrow (less difficulty). You can find a random integer generator here.

• Each group has a set of functions, each of which takes an input and produces an output. I can start with the number `4`, for example, and give it to the function `add6`. What will the output be? (10!)

• I can also compose functions, meaning that the output of one is immediately passed into another. For example, I could compose `add6` and `double`, so the `10` gets passed into the next function, and doubled to produce `20`. What would happen if I composed `add6` with `double` and with `half`? (10!)

• For each of the starting numbers on the board, your job is to figure out which functions to compose in order to get to the end. You will need to use some functions more than once, and that’s ok!

Give students time to experiment with this. You can make the activity more challenging by asking them to find the shortest path from start to end, using the smallest number of compositions. If two groups come up with different compositions that achieve the same end result, have them share their ideas!

### Investigate

The contracts page in your workbook is just like the Function Cards from this activity. Your job as a programmer is to figure out how to compose those functions to get where you want to go, in the most clever or elegant way possible.

Have students open to Composing Image Functions (Page 13). Students create a text image of their name and experiment with their choice of these new functions.

While students are exploring, be available for support but encourage student discussion to solve problems. Make sure students are using the Definitions area (left side) for code they want to keep and are using the Interactions area (right side) to test code or try out new ideas.

Many questions can be addressed with these responses:

• Did you try drawing the Circle of Evaluation first?

• Did you check the contract?

• Have you pressed the Run button to save your Definitions changes?

### Synthesize

• What do all of these functions have in common? They all produce images, they all change some element of the original image

• Does using one of these functions change the original image? No, it creates a whole new image

• What does the number in `scale` represent? The scale factor, the percent by which the image should grow or shrink

• What does the number in `rotate` represent? The rotation angle, measured counterclockwise

• Suppose I wrote the code `scale(3, star(50, "solid", "red"))`.

What’s another line of code I could write that would produce the exact same image?*

`star(150, "solid", "red")`

• The domain and range for `flip-horizontal` is Image -> Image. Why can I use the `text` function as an input for `flip-horizontal`? Because the `text` function produces an Image, which is then used as the input for `flip-horizontal`.

 Strategies for English Language Learners MLR 1 - Stronger and Clearer Each Time: As an alternative, display the discussion questions during the last 5 minutes of the Explore and ask students to discuss the questions with their partner, asking each other for explanation and details and coming up with the clearest, most precise answer they can. Student pairs can then share with another pair and compare their responses before moving into a full class discussion.

## 🔗Decomposing Image Problems 25 minutes

### Overview

Students are given (simple, highly-structured) word problems involving creating images, and must map from the word problems to the names and order of functions needed to solve them. At this stage, the skill is quite brittle and hardly resembles the generalized problem-decomposition skill needed to solve complex word problems in algebra. This is merely the first introduction, and other lessons will deepen and broaden the idea.

### Launch

Create the Circles of Evaluation and write the code for the following images. Write a new line of code for each exercise.

• a solid, green `star` of size 50

• a solid, green `star` that is 3 times as large as the original (using the `scale` function)

• a solid, green `star` that is ½ the size of the original (using the `scale` function)

• a solid, green `star` of size 50 that is rotated 45 degrees (using the `rotate` function)

• a solid, green `star` that is 3 times as large as the original and rotated 45 degrees.

### Investigate

Students complete Function Composition — Practice (Page 14), practicing drawing Circles of Evaluation and writing code with their partner using different functions.

When students are finished, check their work, and ask them to change the color of all of the stars to “gold” or another color of your choosing.

Create an Image that uses the text function and at least 3 of the following functions:

• `rotate`

• `scale`

• `overlay`

• `flip-horizontal`

• `flip-vertical`

• any other image producing function (`triangle`, `star`, `circle`, `rectangle`, etc..)

Students should practice writing comments in the code to describe what is being produced.

Use `#` at the beginning of a line to write a comment.

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). Bootstrap:Integrated Oklahoma by Jen Poole is licensed under a Creative Commons 4.0 Unported License. Based on a work at www.BootstrapWorld.org. Permissions beyond the scope of this license may be available by contacting schanzer@BootstrapWorld.org.