Overview

Students analyze the behavior of a piecewise function

Learning Objectives

Students learn the concept of piecewise functions

Students learn about conditionals (how to write piecewise functions in code)

Evidence Statements

Students will understand that functions can perform different computations based on characteristics of their inputs

Students will begin to see how Examples indicate the need for piecewise functions

Students will understand that

condstatements capture pairs of questions and answers when coding a piecewise function

Product Outcomes

Standards

A-SSE.1-2: Interpret the structure of expressions

F-IF.1-3: Understand the concept of a function and use function notation

F-IF.4-6: Interpret functions that arise in applications in terms of the context

F-IF.7-9: Analyze functions using different representations

F-LE.5: Interpret expressions for functions in terms of the situation they model

Materials

Computers w/ DrRacket or WeScheme

Student workbook

Pens/pencils for the students, fresh whiteboard markers for teachers

Class posters (List of rules, basic skills, course calendar)

Language Table (see below)

Preparation

"Luigi’s Pizza" [LuigisPizza.rkt from source-files.zip \| WeScheme] preloaded on students’ machines, and on the projector

Complete Luigi’s Pizza Worksheet.

Review students’ answers to the exercise.

The code for the

costfunction is written below:Up to now, all of the functions you’ve seen have done the same thing to their inputs:

green-trianglealways made green triangles, no matter what the size was.

safe-left?always compared the input coordinate to -50, no matter what that input was.

update-dangeralways added or subtracted the same amountand so on...

This was evident when going from EXAMPLEs to the function definition: circling what changes essentially gives away the definition, and the number of variables would always match the number of things in the Domain.

It may be worthwhile to have some EXAMPLEs and definitions written on the board, so students can see this point illustrated.

The

costfunction is special, because different toppings can result in totally different expressions being evaluated. If you were to circle everything that changes in the example, you would have the toppings circles and the price. That’s two changeable things, but the Domain of the function only has one thing in it!Of course,

priceisn’t really an independent variable, since the price depends entirely on thetopping. For example: if the topping is"cheese"the function will simply produce9.00, if the topping is"pepperoni"the function will simply produce10.50, and so on. The price is still defined in terms of the topping, and there are four possible toppings - four possible conditions - that the function needs to care about. Thecostfunction makes use of a special language feature called conditionals, which is abbreviated in the code ascond. Each conditional has at least one clause. Each clause has a Boolean question and a result. In Luigi’s function, there is a clause for cheese, another for pepperoni, and so on. If the question evaluates totrue, the expression gets evaluated and returned. If the question isfalse, the computer will skip to the next clause.Look at thecostfunction:

How many clauses are there for the

costfunction?Identify the question in the first clause.

Identify the question in the second clause.

Square brackets enclose the question and answer for each clause. When students identify the questions, they should find the first expression within the square brackets. There can only be one expression in each answer.

The last clause in a conditional can be anelseclause, which gets evaluated if all the questions arefalse.In the

costfunction, what gets returned if all the questions are false? Why is it a good idea to have anelseclause?

Elseclauses are best used as a catch-all for cases that you can’t otherwise enumerate. If you can state a precise question for a clause, write the precise question instead ofelse. For example, if you have a function that does different things depending on whether some variablexis larger than5, it is better for beginners to write the two questions(> x 5)and(<= x 5)rather than have the second question beelse. Explicit questions make it easier to read and maintain programs. When you useelse, someone has to read all the previous questions to know what conditionelsecorresponds to: they can’t just skim all the questions to find the one that matches their situation. This might be counterintuitive to those with prior programming experience, but it does help make code more readable and understandable.Functions that use conditions are called piecewise functions, because each condition defines a separate piece of the function.

Why are piecewise functions useful? Think about the player in your game: you’d like the player to move one way if you hit the "up" key, and another way if you hit the "down" key. Moving up and moving down need two different expressions! Without

cond, you could only write a function that always moves the player up, or always moves it down, but not both.