Defining Functions (Time 20 minutes) Defining a value is helpful when a program has lots of identical expressions. Sometimes, however, a program has expressions that aren t identical, but are just very similar. A program that has fifty solid, green triangles can be simplified by defining a single value, as long as they are all the same size. But what if a program has fifty green triangles of different sizes? [Video] Think about the Image functions you have already used, like star and circle. They take inputs and produce images. Similarly, we might want a green-triangle function that takes the size as an input and produces a green triangle. The programming language doesn t provide this function, but it does let you define your own functions. We want to define our own function (let s call it gt, for green triangle) that takes in a Number and produces a solid green triangle of whatever size we want. (gt 10) would be a shortcut for (triangle 10 "solid" "green") (gt 20) would be a shortcut for (triangle 20 "solid" "green") (gt 1980) would be a shortcut for (triangle 1980 "solid" "green") (gt 98) would be a shortcut for (triangle 98 "solid" "green") and so on... To make this more concrete, have a student "act" as gt. To call the function, another student says "gt ten!" (calling out both the name of the function and the input). The actor responds "triangle ten solid green", to signify the work that the function does when it receives an input. Problems that require a function definition can be phrased as a word problem such as the following: Define a function gt, which takes in a Number and produces a solid, green triangle of the given size. Luckily, we can follow specific steps to define functions from word problems. Let s work through the steps to define gt. Page 11 of 19
Step 1: Write the Contract The first step in defining a function is to write its Contract. Contracts summarize three pieces of essential information about a function: The Name of the function: in this case, the name is given, as gt The Domain of a function, which is the types of data that the function expects: in this case, just a single Number. The Range of this function, which is the type of data that the function produces: in this case an Image since it produces solid, green triangles Here s the gt Contract written as code. The line starts with a semicolon, followed by the name, a colon, the Domain, an arrow, then the Range: It is often a good idea to give students examples of different word problems, and have them pick out the contract for each one. Contracts are written as comments in Racket: whenever Racket sees a semicolon, it ignores the rest of the line after the semicolon. This means that you will never get an error message from Racket for a malformed comment. That also means that you have to check your students contracts more closely, because the computer will not check anything about them (format or contents). Word problems give several clues as to the name, Domain, and Range of a Be sure to read the problem carefully! Some word problems will describe functions that take multiple inputs in their Domain, or inputs of different types. Open your workbook to Page 9, where it says "fast functions", and write the Contract for the gt Page 12 of 19
Step 2: Give Examples It s always a good idea to think through a few examples before defining the Examples show the shortcut that a function is trying to provide. This programming language provides a special construct, called EXAMPLE, which helps you write down how the function is used and what it should compute. You can see two such examples here, written under the contract: (EXAMPLE (gt 100) (triangle 100 "solid" "green")) These examples tell the computer that writing (gt 50) should produce the same result as (triangle 50 "solid" "green"), and that (gt 100) is equivalent to (triangle 100 "solid" "green"). The word problem specifies that the examples must use the name gt, and must all produce solid, green triangles. In your workbook, write two examples of your own for this Be sure to point out that EXAMPLE is capitalized, and that all examples are written in the definitions area. Many students will follow along here without really understanding, simply by patternmatching. Be sure to ask them lots of questions, to have them justify each step: Why does the example have to start with gt? (Because it s the Name of the function, specified in the contract) How do we know gt requires only one number? (Because it s the Domain of the function, specified in the contract) How do we know to use triangle? (Because the word problem tells us what shape it has to produce) How do we know the triangle has to be solid and green? (Because the word problem tells us what shape it has to produce) How do we know the correct order for the inputs to triangle? (The contract for triangle tells us) One of the big ideas here is that each step informs the subsequent step. Make sure to explicitly connect them for students, pointing out that the Contract gives strong hints about how to write each part of the examples. Page 13 of 19
Programmers often write several examples for each Examples like these are a way for a programmer to think through their work. Examples also make it easy to look at what parts of the expression can change, or vary, depending on the inputs. Write the following examples on paper and circle the parts of the examples that are different: (EXAMPLE (gt 100) (triangle 100 "solid" "green")) Pay close attention to what students circle: they should circle something in each part of the Example (the function use on the left and the expression on the right); they should also use the same name for the same concept across the expressions. Their circles will correspond to the variables in their functions, so the variables should appear in both the left and the right sides of the Example. Once you know which parts of the expression change, label the circles with a name that describe their contents. For example, in our two gt examples, we have circled the size of the triangle. Your circled and labeled expressions should look like the following diagram: Step 3: Define the function After writing the Contract, two Examples, and the labeled circles, defining the function itself is relatively simple. Copy everything that stays the same (everything that wasn t circled) in one of your EXAMPLE lines (onto paper or into your editor) In place of each circle, write the label you gave to that circle Change EXAMPLE to define This can be a good opportunity to point out that the parts of the examples that were changeable (or vary-able) are what determines when we need to use the variable. (EXAMPLE (gt 100) (triangle 100 "solid" "green")) (define (gt size) (triangle size "solid" "green")) Page 14 of 19
On your paper, define the gt function, then type the Contract, Examples and Definition into the Definitions area. Click "Run", to have the computer read this definition. Use the function you ve defined, by typing (gt 100) in the Interactions area. Try using the function with different Numbers Your answer should look something like this.: http://www.wescheme.org/openeditor?definitionstext=; gt : Number - > Image%0A %0A (EXAMPLE (gt 95) (triangle 95 "solid" "green"))%0a (define (gt size) (triangle size "solid" "green")) (EXAMPLE (gt 95) (triangle 95 "solid" "green")) (define (gt size) (triangle size "solid" "green")) ] Page 15 of 19
These steps are knows as the Design Recipe, which is a powerful tool for defining functions based on word problems. Practice: Write a function bc, which takes in a Number and produces a solid, blue circle of the given size. In your workbook (still on Page 9), fill out the Contract for this What is the function s Name? What is the function s Domain? What is the function s Range? Using the Contract you ve written, write two Examples for the What part of the Contract helps you fill in the left side of an Example? What part of the Contract tells you what the function needs as an input? How can the Range of a function help you write the Example? Looking at those two examples, circle the parts that are changeable, then label them with a good variable name. Is the variable name you chose the same as the one you chose for gt? Why or why not? Why is it helpful to choose a variable name before defining the function? Now write the function definition, using the Examples you ve written. You will want to explicitly connect each step in the Design Recipe to every other step. Ask students to justify each part of their Contract by referring back to the Word Problem, to justify each step of their Examples by referring back to the Word Problem and Contract, and finally to justify each step of the definition by referring to the Examples. The same variable name can be used in multiple functions, just as in math (where many functions use x as the variable name, for example) Thinking through the word problem step-by-step, we arrive at: ; bc : Number -> Image (EXAMPLE (bc 16) (circle 16 "solid" "blue")) (EXAMPLE (bc 421) (circle 421 "solid" "blue")) (define (bc radius) (circle radius "solid" "blue")) Page 16 of 19
Practice: Write a function dot, which takes in a Color and produces a solid circle of the given color, with a radius of 15. In your workbook (still on Page 9), fill out the Contract for this What is the function s Name? What is the function s Domain? What is the function s Range? Using the Contract you ve written, write two Examples for the function, then circle and label the variables. What part of the Contract helps you fill in the left side of an Example? What part of the Contract tells you what the function needs as input? How can the Range of a function help you write the Example? What is a good variable name for what changes between these Examples. Now write the function definition, using the Examples you ve written. Thinking through the word problem step-by-step, we arrive at: ; dot : String -> Image (EXAMPLE (dot "red") (circle 15 "solid" "red")) (EXAMPLE (dot "blue") (circle 15 "solid" "blue")) (define (dot color) (circle 15 "solid" color)) Page 17 of 19