Unit 2Defining Values and Functions
Unit Overview

Students choose their dataset, and begin to explore. In the process, they learn how to write their own definitions, first defining static values and then complete functions. They are also introduced to the Design Recipe: a structured approach to solving word problems and defining functions.

English

Product Outcomes:
Standards and Evidence Statements:

Standards with prefix BS are specific to Bootstrap; others are from the Common Core. Mouse over each standard to see its corresponding evidence statements. Our Standards Document shows which units cover each standard.

• BS-DR.1: The student is able to translate a word problem into a Contract and Purpose Statement

• given a word problem, identify the domain and range of a function

• given a word problem, write a Purpose Statement (i.e. - rewrite the problem in their own words)

• BS-DR.2: The student can derive test cases for a given contract and purpose statement

• given a Contract and a Purpose Statement, write multiple examples or test cases

• identifying correct and incorrect test cases for a function, based on its contract

• BS-PL.3: The student is able to use the syntax of the programming language to define values and functions

• defining and retrieving values

• writing test cases

• defining and using functions

Length: 90 Minutes

Materials:
Preparation:
 Types Functions Values Number num-sqrt, num-sqr 4, -1.2. 2/3 String string-repeat, string-contains "hello" "91" Boolean true false Image triangle, circle, star, rectangle, ellipse, square, text, overlay

Review

Overview

Learning Objectives

Evidence Statementes

Product Outcomes

Materials

Preparation

Review (Time 10 minutes)

• ReviewLet’s take a look at a real dataset!
• Open the Animals Spreadsheet in a new tab. Take a moment to look around. What do you think this table is for?

• This is some data from an animal shelter, listing animals that have been adopted. We’ll be using this as an example throughout the course, but you’ll be applying what you learn to a dataset you choose as well.

• Open up the Animals Dataset starter file in a new tab. Click "Connect to Google Drive" to sign into your Google account, and then click the "Save as" button. This will save a copy of the file into your own account, so that you can make changes and retrieve them later.

• This file contains some new code that you haven’t seen before. As with our Shapes file, we have a few include lines which import useful libraries for our course. This time, we also include a library that lets us work with Google Sheets:

• On line 9, the load-spreadsheet function is used to load our animal shelter spreadsheet from Google Drive, and give that file a name: shelter-sheet. After that, we see the following code:   Just as you saw with our shapes example, this code defines a new table. This time, it’s called animals-table, and it’s loaded from our Google Sheet. On line 12, you can see the names we are giving to each of the columns, called name, species, gender, age, fixed, legs, pounts and weeks.

Have students look back at the column names in the Google Sheet, and in the load-table function. Point out that they refer to the same columns, even though they have different names!

• Click "Run", and type animals-table into the Interactions Area to see what this table looks like.

• How many columns does this table have?

• For each column, is the data quantitative or categorical?

• For each column, what datatype is being used? Numbers? Strings? Images? Booleans?

• How could you get row for the animal named "Toggle"?

• How could you get the age of the animal named "Toggle" from that row?

• How could you get the species of the animal named "Fritz"?

• How could you get the number of legs of the animal named "Mittens"?

Use the last four questions to review get-row and row-accessors (introduced in Unit 1) before proceeding. Review with the whole class.

• Turn to Page 7 in your Student Workbook, and fill in the table in Question 2.

• Are cats more popular than dogs? Do older animals take longer to get adopted? What are some questions someone might have about this dataset? Write down three questions you have on Page 7.

Defining Values

Overview

Learning Objectives

Evidence Statementes

Product Outcomes

Materials

Preparation

Defining Values (Time 10 minutes)

• Defining ValuesAs you’ve seen, Pyret allows us to define names for values using the = sign. In math, you’re probably used to seeing definitions like $x = 4$, which defines the name x to be the value 4. Pyret works the same way, and you’ve already seen two names defined in this file: shelter-sheet and animals-table. We generally write definitions on the left, in the Definitions Area. You can add your own definitions, for example:

• Define a value with name food, whose value is a String representing your favorite food

• Define a value with name year, whose value is a Number representing the current year

• Define a value with name color, whose value is an Image drawn in your favorite color

• Define a value with name likes-cats, whose value is a Boolean that is true if you like cats and false if you don’t

• Each row of our animals-table represents a single animal in our shelter. We can use the get-row function from yesterday to define values. Type the following lines of code into the Definitions Area and click "Run":   What happens when you evaluate mittens in the Interactions Area?

Define values for mittens and sasha. Then, select two pets from the animals-table that you would most like to adopt, and two more than you would least like to adopt (don’t worry, those animals will find homes too!). What rows are they? Define four additional values for these animals, using the pet’s names and rows.

Defining Functions

Overview

Learning Objectives

Evidence Statementes

Product Outcomes

Materials

Preparation

Defining Functions (Time 40 minutes)

• Defining FunctionsSuppose you work at the animal shelter, taking care of all the animals who live there. You want to make sure they’re healthy, happy, and find good homes. For each animal, you might want to ask certain questions:

• What kind of animal is it?

• Has it been fixed?

• When was it born?

• Is it a kitten?

• Suppose I want to know if mittens is fixed or not.

Quick! Is mittens fixed? Describe what you did to find the answer.

We know lots of functions in Pyret that can handle Numbers, Strings, Images and so on, but none that can handle animals! We need to build our own.

• To build our own functions, we’ll use something called the Design Recipe. The Design Recipe is a way to think through the behavior of a function, to make sure we don’t make any mistakes with the animals that depend on us! The Design Recipe has three steps, and we’ll go through them together for our first function.

Turn to page Page 8 in your Student Workbook.

• The first thing we do is write a Contract for this function. You already know a lot about contracts: they tell us the Name, Domain and Range of the function. Our function tells us if an animal is fixed or not, so we’ll call it is-fixed. It consumes an animal (represented by a Row in our table), and produces a Boolean: true if the animal is fixed, and false if it isn’t. A Purpose Statement is just a description of what the function does:

• You already know how to write examples for built-in functions, where we write the answer we expect to get back. Just as we did before, we start with the name of the function we’re writing, followed by an example input. Let’s use some of the pets we defined earlier as examples.

Make sure students see that these two examples are equivalent. Walk through this first example carefully. Make sure students understand where the birth-year came from the Name in our contract, and that mittens came from the Domain in our contract. 2018 - mittens["age"] came from our purpose statement, and the label also came from the variable name in our contract.

• Write another example that shows your work in this block, using the fritz value that you defined earlier.

• In the examples where we show our work, do you notice a pattern? Most of the code for these examples is exactly the same, and only a small bit is changing: mittens and fritz.

• Circle all of the parts in your example block that are changing.

• What does the stuff you circled represent? Are mittens and fritz years? Legs? No - they are animals! Let’s label them...

• After having written our examples, this part is easy! The part of the examples before is tells us how to begin. We start with the fun keyword (short for "function"), followed by the name of our function and a set of parentheses. This is exactly how all of our examples started, too. But instead of writing mittens, we’ll use the label that we gave it. Then we add a colon (:) in place of is, and continue to follow our examples, replacing anything we circled with the label. Finally, we finish with the end keyword.

• Now that we’ve defined our function, we can click "Run" and actually use it!

After you’ve clicked run, try typing in the following expressions, and see what happens:

• Our examples: block is a helpful way to check our work, so we don’t make mistakes. Suppose we had a typo in our function definition, and added instead of subtracted:

Be sure to check students’ contracts and purpose statements before having them move on to their examples!

• So far, our functions have been pretty simple: they consume a row, and they produce one column from that row as-is. But supposed we want to use that value to find out specifically whether or not an animal is a cat, or whether it’s a child? Let’s walk through a more complex Design Recipe together, by turning to Page 9.

• Define a function called is-cat, which consumes a row from the animals-table and returns true if the animal is a cat.

• What is the name of this function? What are it’s Domain and Range?

• Is Sasha a cat? What did you do to get that answer?

Have students explain their thinking carefully, step-by-step. Repeat this with other animals.

• To find out if an animal is a cat, we look at the species column and check to see if that value is equal to "cat". This gives us out first example:

Add a second example, this time for an animal that is not a cat.

Have students share their examples. Point out that the code is the same for all examples, aside from the name of the animal being tested.

• Just we’ve done before, let’s look at our examples and circle the things that are change from one to the other.

• Do all our examples use is-cat?

• Do all our examples use the same inputs?

• Do all our examples look at the same column?

• Do all our examples compare that column value to "cat"?

What label should we use here?

Make sure students realize that the label is specified in the Domain.

• As before, we’ll use the pattern from our examples to come up with our definition.

• What is the function name?

• What is the name of the variable(s)?

• What do we do in the body in the function?

• Type this definition - and its examples! - into the Definitions Area, then click "Run" and try using it.

• Now let’s practice writing functions that do things to their input. This time, you’ll have to write the Contract, Purpose Statement, and first example yourself!

Use the bottom half of Page 9 to solve the following problems:

• Define a function called is-young, which consumes a Row of the animals-table and produces true if its age is less than 2.

Show students that they can combine all their examples into a single block at the top of the file.

• Turn to Page 10, and see if you can solve the following word problems:

• Define a function called nametag, which prints out each animal’s name in big red letters.

• Define a function called is-kitten, which consumes a Row of the animals-table and produces true if it’s a cat and is less than 2 years old.

OPTIONAL: Show students how to write is-kitten using the is-cat and is-young functions they defined before.

Overview

Learning Objectives

Evidence Statementes

Product Outcomes

Materials

Preparation

Choose Your Dataset (Time 25 minutes)

Closing

Overview

Learning Objectives

Evidence Statementes

Product Outcomes

Materials

Preparation

Closing (Time 5 minutes)

• Closing Building functions is a powerful technique, which you’ll use throughout the course. Today, you learned how to write functions that work on one row of a table at a time. In the next lesson, you’ll learn how to use those functions to loop over an entire table, letting us extend, filter, and sort our animals-table

Make sure to save your work. Hit the Save button in the top left. This will save your program in the code.pyret.org folder within your Google Drive.

If your students are working in pairs/groups, make sure that each student has access to a version of the program. The student who saved the program to their Google Drive can share their program with anyone by hitting the Publish button in the top left, choosing "Publish a new copy", then clicking the "Share Link" option. This will allow them to copy a link to the program, then send to their partners in an email/message.