Contracts provide a way for students to better understand and discuss functions. Through this lesson, students will look at known functions and come up with the contracts that describe those functions.
Describe a function in terms of its name, domain, and range.
Create contracts for arithmetic and image-producing functions.
Common Core Math Standards
- F.IF.1: Understand that a function from one set (called the domain) to another set (called the range) assigns to each element of the domain exactly one element of the range. 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. The graph of f is the graph of the equation y = f(x).
Additional standards alignment can be found at the end of this lesson
What's in a Contract
- Describe a function in terms of its name, domain, and range
- Create contracts for arithmetic and image-producing functions
Materials, Resources, and Prep
For the Student
For the Teacher
This lesson has three new and important words:
- Contract - a statement of the name, domain, and range of a function
- Domain - the type of data that a function expects
- Range - the type of data that a function produces
You’ve already seen several functions that take in two Numbers, such as +, and -. Other functions like "star", take in a Number and two Strings. Different functions take in different inputs, and we need a way to keep track of the requirements for each function.
- What does the '+' function do?
- What does it take as input?
- What does it return as output?
- How about the 'triangle' function?
- What do these different functions have in common?
Let's look at a simple way to describe any function, it's called a "contract"
- What is a Contract?
- A formal agreement
- A description of expected behavior
- What do Contracts tell us?
- What a function should do
- What inputs a function needs
- What a function returns
Encourage students to think about contracts in the real world. What purpose do they serve? If a contract is signed, do we expect it to be followed?
Contracts have three distinct parts:
The Name of a function briefly describes what the function does.
The Domain of a function is the data that the function expects.
The Range of a function is the data that the function produces.
By keeping a list of all the functions in a language, and their Domains, programmers can easily look up how each function is used. However, it’s also important to keep track of what each function produces! For example, a program wouldn’t use "star" if they were trying to produce a Number, because star only produces Images.
Domains and Ranges help programmers write better code, by preventing silly mistakes and giving themselves hints about what to do next. A programmer who wants to use "star" can look up the Domain and immediately know that the first input has to be a Number (like 100), without having to remember it each time. Instead of writing a single value there, a programmer could write a whole expression, like (25 * 4). We know this code will return an appropriate value (Number) by looking at the Range for *; therefore, the result of * can be used in place of any Number value.
When programmers write down the Domains and Ranges of each function, they write what are called contracts, to keep track of what each function needs.
Let's look at a few example contracts - for each contract we'll identify the Name, Domain, and Range
- +: Number Number -> Number
- triangle: Number String String -> Image
- rotate: Number Image -> Image
Let's see if we can come up with contracts for some of the functions you've already seen. You'll want to make sure that you've got your contract log, as this is where you'll keep a running document of all contracts you write - both for existing functions and ones of your own creation.
- We'll start with contracts for simple arithmetic functions
- +, -, *, /
Those were pretty easy as arithmetic functions only deal in Numbers. When it comes to writing functions that deal with multiple data types, looking at the Evaluation Block can give us some helpful clues.
- The Name of each function is at the top
- There will be a slot for each Domain element
- The color of each slot tells you Domain type
- The color of the whole block tells you Range
- Color codes: Number String Image
Common mistakes when students first write down contracts include: writing values (such as "red") instead of types (such as "String") and forgetting arguments. Read your students’ contracts carefully, as they often indicate misconceptions that will persist and affect them later on.
Display each of the following Evaluation Blocks and ask students:
- What is the Name of this function?
- What is the Domain of this function?
- What is the Range of this function?
- Add this function's contract to your reference
As you continue programming, make sure that you document a contract for every new function you encounter or write. In the next unit, you’ll learn how to create your own functions to save work in writing expressions (this will turn out to be an essential part of writing a game). You’ll also start customizing your game with images for the elements in your game design.
Common Core Math Standards
- 5.OA.1 - Use parentheses, brackets, or braces in numerical expressions, and evaluate expressions with these symbols.
- 5.OA.2 - Write simple expressions that record calculations with numbers, and interpret numerical expressions without evaluating them. For example, express the calculation “add 8 and 7, then multiply by 2” as 2 × (8 + 7). Recognize that 3 × (18932 + 921) is three times as large as 18932 + 921, without having to calculate the indicated sum or product.
- 6.NS.8 - Solve real-world and mathematical problems by graphing points in all four quadrants of the coordinate plane. Include use of coordinates and absolute value to find distances between points with the same first coordinate or the same second coordinate.
- 6.EE.9 - Use variables to represent two quantities in a real-world problem that change in relationship to one another; write an equation to express one quantity, thought of as the dependent variable, in terms of the other quantity, thought of as the independent variable. Analyze the relationship between the dependent and independent variables using graphs and tables, and relate these to the equation. For example, in a problem involving motion at constant speed, list and graph ordered pairs of distances and times, and write the equation d = 65t to represent the relationship between distance and time.
- 7.EE.4 - Use variables to represent quantities in a real-world or mathematical problem, and construct simple equations and inequalities to solve problems by reasoning about the quantities.
- 8.F.1 - Understand that a function is a rule that assigns to each input exactly one output. The graph of a function is the set of ordered pairs consisting of an input and the corresponding output.1
- 8.F.2 - Compare properties of two functions each represented in a different way (algebraically, graphically, numerically in tables, or by verbal descriptions). For example, given a linear function represented by a table of values and a linear function represented by an algebraic expression, determine which function has the greater rate of change.
- A.SSE.1 - Interpret expressions that represent a quantity in terms of its context.
- A.SSE.2 - Use the structure of an expression to identify ways to rewrite it. For example, see x4 – y4 as (x2)2 – (y2)2, thus recognizing it as a difference of squares that can be factored as (x2 – y2)(x2 + y2).
- F.IF.1 - Understand that a function from one set (called the domain) to another set (called the range) assigns to each element of the domain exactly one element of the range. 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. The graph of f is the graph of the equation y = f(x).
- F.IF.2 - Use function notation, evaluate functions for inputs in their domains, and interpret statements that use function notation in terms of a context.
- F.IF.3 - Recognize that sequences are functions, sometimes defined recursively, whose domain is a subset of the integers. For example, the Fibonacci sequence is defined recursively by f(0) = f(1) = 1, f(n+1) = f(n) + f(n-1) for n ≥ 1.
Common Core Math Practices
- MP.1 - Make sense of problems and persevere in solving them.
- MP.2 - Reason abstractly and quantitatively.
- MP.3 - Construct viable arguments and critique the reasoning of others.
- MP.4 - Model with mathematics.
- MP.5 - Use appropriate tools strategically.
- MP.6 - Attend to precision.
- MP.7 - Look for and make use of structure.
- MP.8 - Look for and express regularity in repeated reasoning.