Students are formally introduced to the steps of the Design Recipe.
Students practice using Contracts to create examples of functions
Students learn to abstract over examples to create functions
Given a definition, students will be able to identify the Name, Type and Value that is defined.
Given a contract and purpose statement for a simple, one-variable 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
Students will use the Design Recipe to define a function, which is used to make a rocket fly.
Pens/pencils for the students, fresh whiteboard markers for teachers
Class poster (List of rules, language table, course calendar)
Language Table (see below)
Student workbook folders with names on covers, and something to write with
Functions 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 11 in your workbook, and read the word problem carefully.
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?
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.
- 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.
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.
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.
- 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 calcluated - 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!