(Also available in WeScheme)
Students learn to combine image transformation functions as well as to describe the order of operations involved in algebraic function compositions such as f(g(h(x))) using Circles of Evaluation.
Lesson Goals 
Students will be able to:

Studentfacing Goals 

Materials 

Supplemental Resources 

Preparation 

Key Points For The Facilitator 

Click here to see the prior unitbased version.
 contract

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

a way of classifying values, such as: Number, String, Image, Boolean, or any userdefined data structure
 definitions area

the leftmost text box in the Editor where definitions for values and functions are written
 image

a type of data for pictures
 interactions area

the rightmost text box in the Editor, where expressions are entered to be evaluated
🔗Composing Functions 10 minutes
Students are given a scaffolded activity that forces them to use the output of one function as the input to another  to compose them.
Launch
Divide students into groups of 34, 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 functionadd6
. 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
anddouble
, so the10
gets passed into the next function, and doubled to produce20
. What would happen if I composedadd6
withdouble
and withhalf
? 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!
Investigate
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.
Synthesize
If two groups come up with different compositions that achieve the same end result, have them share their ideas!
🔗Diagramming Function Composition
Overview
The Circles of Evaluation are extended to provide a visualspatial metaphor for function composition, in addition to Order of Operations.
Launch
Three of the function cards we just used were for the functions f
, g
and h
:

f
multiplied its input by 3 
g
added six to its input 
h
subtracted one from its input
We can compose those function in any order. If we composed them as f(g(h(x)))
and evaluated them for x = 4
what would happen?
We can diagram the function composition using Circles of Evaluation (see first column, below). In the second column, we’ve replaced the function names in each Circle of Evaluation with what each function does:
Function Composition  Order of Operations  

(f (g (h x))) 
(* 3 (+ ( x 1) 6)) 
The circles show us that in order to evaluate f(g(h4)))

First we would have to evaluate h4, subtracting
1
from4
to get3

Then we would evaluate g3, adding
6
to3
to get9

Then we would evaluate f27, tripling
9
to get27
Investigate
Have students turn to Diagramming Function Composition to practice writing, translating and evaluating Circles of Evaluation of composed functions.
Synthesize
Do f(g(hx)) and g(h(fx)) evaluate to the same thing? No!
Why not? order matters!
🔗Composing Functions in Code 20 minutes
Overview
The Circles of Evaluation are extended to functions that do more than compute values.
Launch
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.
Investigate
Have students log into code.pyret.org (CPO) open a new program and save it as Function Composition.
Have students open to Function Composition — Green Star, in which they will be drawing circles of evaluation to help them write expressions to compose a series of images.

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.

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.
Then have students open to Function Composition — Your Name in which they will create a text image of their name and experiment with other functions.
Strategies for Facilitation While students are exploring, be available for support but encourage student discussion to solve problems. Many student 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? Encourage students to practice writing comments in the code to describe what is being produced, using 
If you have time, you can also have students work with Function Composition — scalexy and/or Function Composition Matching Activity (Desmos)
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 by which the image should grow or shrink

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

The Domain and Range for fliphorizontal is Image > Image. Why can I use the output of the text function as an input for fliphorizontal? Because the text function produces an Image, which is then used as the input for fliphorizontal.
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. 
Fun with Images! Now that students have learned how to use all of these imagecomposing functions, you may want to give them a chance to create a design of their own, tasking them with using at least 4 functions to create an image of their choosing. Our Flags lesson also dives deeper into image composition. 
🔗Composing Multiple Ways Optional
Overview
Students identify multiple expressions that will create the same image, and think about the merits of one expression over another.
Launch
As is often true with solving math problems, there is more than one way to get the same composed image.
Suppose I wrote the code: scale(3, circle(50, "solid", "red")).
What’s another line of code I could write that would produce the exact same image? circle(150, "solid", "red")
Investigate
Students complete More than one way to Compose an Image!.
Synthesize
There is a special function in code.pyret.org (CPO) that let’s us test whether or not two images are equal.
isimage=:: Image, Image > Boolean
Use it to test whether all of the expressions you wrote successfully build the same images.

Could we have written more expressions to create the same images?

Are all of the ways to write the code equally efficient?
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.