Students will use the Design Recipe to define a function, which is used to make a rocket fly.
Students will write functions to solve simple problems by using the Design Recipe
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.
8.F.1-3: The student defines, evaluates, and compares functions
comparison of properties of two functions each represented in a different way (algebraically, graphically, numerically in tables, or by verbal descriptions)
A-SSE.1-2: The student interprets the structure of expressions to solve problems in context
interpretation of expressions that represent a quantity in context
interpretation of terms, factors, and coefficients of an expression
F-BF.3-4: The student builds new functions from existing functions.
inversion of a function
F-IF.1-3: The student uses function notation to describe, evaluate, and interpret functions in terms of domain and range
description of a function using terms domain and range
explanation that if f is a function and x is an element of its domain, then f(x) denotes the output of f corresponding to the input x
evaluation of functions using function notation for inputs in their domains
interpretation of statements that use function notation in terms of a context
F-IF.4-6: The student interprets the behavior of functions that arise in applications in terms of the context
interpretation of a relationship between two quantities in a table or graph in terms of key features
relation of the domain of a function to its graph and, where applicable, to the quantitative relationship it describes
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
given multiple examples, identify patterns in order to label and name the variables
BS-DR.3: Given multiple test cases, the student can define a function
given examples and labeled variable(s), define the function
Length: 110 Minutes
Glossary:
contract: a statement of the name, domain, and range of a function
design recipe: a sequence of steps that helps people document, test, and write functions
domain: the type of data that a function expects
example: shows the use of a function on specific inputs and the computation the function should perform on those inputs
name: how we refer to a function or value defined in a language (examples: +, *, star, circle)
purpose statement: a brief description of what a function does
range: the type of data that a function produces
type: refers to a general kind of data, like Number, String, Image, or Boolean
variable: something that changes
Materials:
Computer for each student (or pair), running WeScheme or DrRacket with the bootstrap-teachpack installed
The Design RecipeFunctions are a key part of animation in computer programs. A function that draws a static picture of a cat,
for example, can place the cat at a different location based on the input. When that input changes slightly
based on time or user-interaction, the cat will appear to move. This is similar to the way that flip-book
animations work, in which each page draws a static image that has changed by a small amount. When the pages
are displayed quickly, the images appear to change smoothly.
Review the importance of definitions for students (defining values helps cut down on redundancy
and makes future changes easier, defining functions allows for simplicity and testability.
Be sure to use vocabulary regularly and carefully, pushing students to use the proper terms
throughout.)
Putting these images together, we arrive at an animation of the rocket moving up the screen.
Let’s see an example of this kind of animation, using a function to make a rocket-blast off! Turn to
Page 12 in your workbook, and read the word problem carefully.
Check student understanding carefully, to make sure students read the problem carefully. It may be helpful to draw a diagram or table showing the change of rocket position on the board, and to have students verbally walk through a few examples.
A rocket blasts off, traveling at 7 meters per second. Write a function called rocket-height that takes in the
number of seconds that have passed since the rocket took off, and which produces the height of the rocket at that time.
What is the rocket-height function taking in as an input? What type of data is that?
What is the function producing as an output? What type of data is that?
What are the three parts of a Contract?
What is the Name of the function you are being asked to define?
What is the Domain of the function?
What is the Range of the function?
The Contract is a way of thinking about the function in a general way, without having to worry about exactly how it will work or how it will be used. Starting with simple questions like these will make later steps much easier to think about. However, the Contract doesn’t always have enough information! The Domain for star, for example, specifies that the function needs a Number and two Strings, but doesn’t mention the fact that the first String must be "solid" or "outline". To add this information, programmers write Purpose Statements, which are simple sentences that explain what a function does.
Underneath the Contract, copy the following simple Purpose Statement for rocket-height.
This is an opportunity to talk about the importance of writing, clarity, and brevity. What information is essential for a purpose statement? What information is irrelevant? A good purpose statement describes what is computed and how its inputs are used; it should go beyond the information given in the contract and implicit in the name of the function.
Armed with the Contract and Purpose Statement, it becomes easy to write an EXAMPLE. Every example begins with the name of the function and a sample input, both of which are written in the Contract. In this case, you know that the function is called rocket-height and that it expects a single number as input. The Purpose Statement goes further, telling you that the input is multiplied by 7. We can use this to write two examples, with different numbers of seconds as inputs.
Note: The example shown above is broken into two lines! As functions and examples become more complex, it will become difficult to squeeze them into a single line. In your workbooks, every example and definition from this point onwards will use the two-line format.
The main idea for students in this activity is to realize that the Contract and Purpose Statement can be relied upon to write examples. Ask students to justify every part of each example, using only the Contract and Purpose Statement. Students are often tripped up by the switch from one-line examples to two-line ones. Be very careful to point this out to students, and to check their workbooks to see where they are writing their examples. At the end of the activity, make sure that students circle ONLY what has changed.
)
In your workbook, write two new examples of rocket-height using different values for the number of seconds. Both examples will have a lot in common (they all use rocket-height as the function name, they all multiply their input by 7, etc). Once you are done writing them, circle only the parts of the examples that are changeable.
By comparing two different examples, it is easy to tell what changes. According to the Purpose Statement, it is the number of seconds that the rocket has been flying.
On your paper, label the items you circled with "seconds".
Have a discussion with students about why "seconds" is a better name than "time". Talk about specificity, relevance, and readability.
Labeling what is changeable gives programmers a sense for the names of a function’s variables. Variables are like placeholders for values that can be different at different times. A function that computed how much you pay for text messages each month, for example, might have a variable for the number of messages that you sent in that month. The number of messages you sent might change from month to month, but each one could still cost 20 cents.
How many variables does rocket-height have? What is the name of each variable? Define the function, using all the information from your Examples, Contract and Purpose Statement.
As with the Examples, ask students to justify each part of the definition. In this case, the function name can be derived from the Contract, and the variable name and function body from the Examples.
The Design Recipe allows a programmer to focus on one step of the problem at a time, and to use previous steps to help complete the next one.
What does the Contract tell a programmer about a function?
What does the Purpose Statement tell a programmer about a function?
How do the Contract and Purpose Statement help a programmer write Examples?
Why is it helpful to circle and label the parts of the Examples that change?
How do all of these steps help a programmer define a function?
You may have noticed that the Examples for rocket-height wrote out the multiplication as (* 11 7),
rather than the actual answer (77). Why bother to show the way a calculation is performed? By doing this,
Examples can provide hints about what process is taking place. In the provided Rocket code (Rocket.rkt from
source-files.zip | or the online file),
you will see why it is so important to show your work when writing examples.
Click "Run", and wait until a window appears with a rocket at the bottom of the screen and numbers for "time" and "height" at the top. This animation is set to update the rocket every time the spacebar is pressed, to simulate time going by. Hit the spacebar a few times, and notice that the time at the top of the window increases. Does the rocket move? Time is passing, but our rocket’s height hasn’t changed! Close the rocket window, so that you can see the code.
The Contract for this function is correct: the function’s Name, Domain and Range are all accurately written here. However, the next step definitely has some problems:
There is only one Example. This makes it difficult to notice what is changing, which could lead to mistakes when identifying variables
The Example doesn’t show how the height is calculated - instead, this programmer just wrote the "answer", without showing their work.
By skipping these steps in the Examples, it can be easy to make mistakes when defining the function.
Can you see the mistake the programmer made?
Without seeing multiple Examples, this programmer failed to realize that the height of the rocket has to be calculated for every input. Instead, they just produce the same number every time (in this case, zero). As a result, the rocket is always at zero, no matter how many seconds have passed.
Fix this programmer’s Example to show their work.
Write a second Example, below the first one.
Fix the definition for the function, so that it multiplies the number of seconds by 7.
Click "Run", and then use the spacebar to make the rocket fly!
Practicing the Design Recipe
Overview
Students use the Design Recipe to define simple functions (of one and two variables), given various word problems
Learning Objectives
Evidence Statementes
Given a contract and purpose statement for a simple function, students will be able to write two Examples.
Given two examples for a simple function, students will be able to identify the variable
Given two examples for a simple function, students will be able to write the definition
Product Outcomes
Students will write functions to solve simple problems by using the Design Recipe
Materials
Preparation
Write agenda on board
Display Class posters, Language Table, Design Recipe
Seating arrangements: ideally clusters of desks/tables
Practicing the Design Recipe(Time 40 minutes)
Practicing the Design RecipeThe Design Recipe is a roadmap for defining functions, which programmers use to make sure the code they write does what they want it to do. Each step builds on the last, so any mistakes can be caught early in the process. This roadmap has a series of steps:
Moving from a word problem to a Contract
Writing Examples based on the contract
Defining a function that matches the examples
Start out by practicing each of these steps, one at a time.
For each of the following word problems, write down the Contract:
Define a function ’purple-star’, that takes in the size of the star and produces an outlined, purple star of the given size.
Define a function ’spot’, that takes in a color and produces a solid circle of radius 50, filled in with that color
To find the average of two numbers, they should be added together and divided by two. Define a function ’average’, which takes in two numbers and produces their average
A company logo is a word drawn in big, red letters, rotated some number of degrees. Define a function ’logo’, that takes in a company name and a rotation, and produces a logo for that company
Be sure to include a good Name for each function, and remember that the Domain and Range can only include types like Numbers, Images, Strings, etc.
[Video] If students come up with different Domains or Ranges for their functions, have them explain their thinking for each one. These conversations are an important part of this step!
A Contract is the foundation for a function, which gives programmers just enough information to use them: the name of the function, the types of data it expects and the type of data it returns.
Now that you’ve completed the Contracts for all four word problems, try to generate two Examples for each one.
Challenge students to defend their Examples (their function name, the number of inputs, their types and the type of the returned value). Make sure that the two Examples for each function have different input values! For each of these questions, students must be able to point to the specific part of their Contract as the justification for their Example.
Once you have two or more Examples, it should be easy to identify what has changed between them. In fact, the number of things that change should match the number of things in the function’s Domain: if the Domain has a Number and a String in it, then those two values should be the things that differ between your Examples.
Identify what has changed between these Examples, and use this information to define all four functions.
Make sure students have chosen good variable names for their function definitions, and ask students to justify every part of the function body. The only acceptable answers should be "I copied this because it’s the same in both Examples", or "I used a variable name because it differs between Examples."
Turn to Page 14 in your workbook, and read the word problem carefully.
Circle the Name of the function, and underline what it takes in and what it produces.
Write the Contract for this function, using what you underlined to help you find the Domain and Range.
Underneath the Contract, write the Purpose Statement by summarizing what the function does in a single sentence
Challenge students to explain why this function does not need to know the color of the square, or whether or not it is solid. They main idea here is that the function already "knows" these things, so the only thing that is changing is the size of the square.
Note that there are actually two easy solutions here: using the square function, or using rectangle with the same variable for width and height.
Remember that the Contract and Purpose Statement can be used to write the Examples, even if a programmer isn’t sure how to begin.
Using only the Contract and Purpose Statement, see if you can answer the following questions:
Every Example begins with the name of the function. Where could you find the name of the function?
Every Example has to include sample inputs. Where could you find out how many inputs this function needs, and what types they are?
Every Example has to include an expression for what the function should do when given an input. Where could you look to find out what this function does?
Write two Examples on your paper, then circle and label what is changing between them.
Make sure students realize that every answer can be found in the Contract or Purpose Statement. Suggestion: write these steps on the board, and draw arrows between them to highlight the process. The goal here is to get students into the habit of asking themselves these questions each time they write Examples, and then using their own work from the previous step to find the answers. NOTE: students can solve this using either square or rectangle. The latter requires them to use the same variable twice, which is a useful thought-exercise.
Once the two Examples are written and the variable (or variables) are labeled, it becomes easy to define the function.
Using the Contract, Purpose Statement and Examples, see if you can answer the following questions:
Every function definition begins with the name. Where did you write this already?
Every definition has to include variable names for the inputs. Where did you write these down?
Every definition should work exactly the way the Examples did. Where could you look to figure out what the function should do?
Write the function definition on your paper.
When typing in a function definition, be sure to type in all three parts: The Contract and Purpose Statement, the Examples and the Definition. When "Run" is clicked, the computer will read the definition, test your Examples to make sure it works correctly, and then add the definition to your language. Once added, you can use the function as if it were built into the computer.
; red-square : Number -> Image
; draw a solid, red square using the given size
(EXAMPLE (red-square 42) (square 42 "solid" "red"))
(EXAMPLE (red-square 73) (square 73 "solid" "red"))
(define (red-square size) (square size "solid" "red"))
Some functions take more than one input. The red-square function always makes solid red squares, with size being the only thing that varies. Suppose, however, we wanted the function to make red squares that could be either solid or outline? This would mean that both the size and the style can vary, so a user might type (red-square 50 "solid") or (red-square 50 "outline")
How would that change the Domain of the function?
How would the Examples have to change?
How would the definition change?
Change each part of the code you typed, to allow red-square to take in "solid" or "outline" as a second argument.
The Design Recipe can be used for functions that take any number of inputs. As a simple example, take the following word problem:
Turn to Page 13 in the workbook, and read the word problem carefully.
Circle the Name of the function, and underline what it takes in and what it produces.
How many things does this function take as input? Are they Numbers? Strings? Images?
Write the Contract for this function, using what you underlined to help you find the Domain and Range.
Underneath the Contract, write the Purpose Statement by summarizing what the function does in a single sentence.
Write two Examples for this function, then circle and label what varies between them.
Write the Definition for this function.
If time allows, pass out extra Design Recipe worksheets [PDF], and have students practice with other word problems. Practice variations on red-square, involving two inputs changing while other parameters stay fixed.
The Design Recipe (Algebra)
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
The Design Recipe (Algebra)(Time 40 minutes)
The Design Recipe (Algebra)The real power of programming isn’t how well you know the language. It’s about how well you can use it to solve problems!
You’ve learned about a powerful tool that helps you take word problems on paper and turn them into functions on the computer:
the Design Recipe! It turns out that the Design Recipe can also be used to help you solve word problems in algebra, too!
Turn to Page 39 in your workbooks, read the word problem carefully, and write down the Contract
for the function:
"A rocket is flying from Earth to Mars at 80 miles per second. Write a function that describes the distance the rocket
has traveled, as a function of time."
The distance a rocket travels is measured in numbers of miles, and time is measured in numbers of seconds. This gives us
the contract:
Just as before, the Contract gives us a strong hint about how to write the Examples. Here is one Example, written in
Racket code: (EXAMPLE (distance 5) (* 5 80)) Here’s the same example, written in algebra:
Write two additional Examples for this function, using Algebraic notation.
Point out that the Contract hasn’t changed at all between Racket and Algebra.
If students are struggling with the change in syntax, let them first write the Examples in Racket, and
then convert them to Algebra by following the model shown here.
As before, a function’s variables can be identified by writing two Examples, and looking at what changes between them.
Using the Examples you’ve written, identify the variable(s) for the function.
Then define the function using Algebraic syntax.
The Design Recipe helps transition from a written description of a function into a formal definition,
Turn to Page 40 in your workbooks, read the word problem carefully, and write
down the Contract for the function: "A rocket is traveling from Earth to Mars
at 80 miles per second. Write a function that describes the time the rocket has been traveling, as
a function of distance."
Point out that this is the same relationship between distance and time as before,
only now we want be able to see the relationship from the opposite direction: time in terms of
distance, rather than distance in terms of time.
Once we have the Contract, it’s easy to write Examples:
Use the Contract to write two Examples for this function, then identify the variable(s) for the function.
How many variables does this function have?
Name the variable(s).
Define this function, using Algebraic syntax.
Once your function is set up, it’s easy to just plug in values and get answers back. With most word problems, the hard part is setting up the function in the first place.Luckily, the Design Recipe makes setting up that function a lot easier! We’ve just used it to set up two different functions, which could be used to give us answers in terms of distance or time. Defining functions is like building tools, which you can use to solve simple problems or combine together to solve more complex ones.
Suppose you wanted to know how far the rocket traveled in 6 seconds: which of the two functions here would you use? What if you wanted to know how long it takes for the rocket to go a thousand miles?
Ask students to identify which function they would use to answer a variety of different questions.
Turn to Page 41 in your workbooks, read the word problem carefully:
"A rocket leaves Earth, headed for Mars at 80 miles per second. At the exact same time, an asteroid
leaves Mars traveling towards Earth, moving at 70 miles per second. If the distance from the Earth
to Mars is 50,000,000 miles, how long will it take for them to meet?"
What are the facts that the problem gives you?
What is the problem asking you for?
Use your answers to complete the following sentence: "Given ____, write a function that produces ____."
If such this function existed, what would be a good name for it?
Ultimately, the problem is asking students about a functional relationship between distance and collision-time.
If we had a function that computed collision-time, it would be easy to answer this question just by plugging in the distance between the Earth and Mars. Rather than start worrying about setting up an equation, you can use the Design Recipe to build the function itself!
Write down the contract for this function, then write two Examples using different distances.
By now, you’ve got the contract for this function and a good purpose statement:
So now we need to figure out what calculations go after the equals sign. Luckily, we have a hint: we know that whatever it is, it must come out to zero, since we know that they’ve already collided when they are zero miles apart.
If the rocket is going 80 miles/second, and the asteroid is going 70 miles/second, how fast are they approaching each other?
, so we know that they are getting closer and closer together at a rate of 150 miles per second. This isn’t that different from our time function: we have some speed, and we want to know how much time it will take to go a given distance. So what do we write for our example?
Write two Examples of your own using different distances, then define the function so it works with any distance.
By now, you may already see the big picture: the distance given in this problem can be plugged into the equation
without having to define the function itself. Sometimes the Design Recipe will get you to the answer without
even having to finish! Other times, however, you may not see the big picture until you’ve broken the problem
down into smaller parts, each with it’s own function.
You can add as many Design Recipe pages as you like to the workbooks, using any algebra problem you like.
We recommend using word problems from your students’ algebra textbook, or even from your state’s standardized test!
Closing
Overview
Learning Objectives
Product Outcomes
Materials
Preparation
Closing(Time 5 minutes)
ClosingIn this unit, you started to see how functions are useful in writing animations: functions help produce information (such as the height of a rocket) that are used to create scenes in a game. With the extra practice writing functions and using the Design Recipe, you are now ready to start writing functions that will move the elements within your own game.
Have students volunteer what they learned in this lesson
Reward behaviors that you value: teamwork, note-taking, engagement, etc