instagram

Students discover that they can make their own functions and are introduced to a structured approach to building them called the Design Recipe.

Lesson Goals

Students will be able to:

  • identify patterns where a function would be useful

  • explain the difference between defined values and functions

  • match examples, contracts, and definitions for the same function

Student-Facing Lesson Goals

  • I can explain why a function is useful.

  • I can connect contracts, examples, and definitions for a function.

Materials

Preparation

  • Make sure all materials have been gathered

  • Decide how students will be grouped in pairs

Key Points for the Facilitator

  • This lesson represents a big shift in thinking. After some practice, students will not be limited to pre-existing functions!

Language Table

Types

Functions

Values

Number

+, -, *, /, expt, sqr, sqrt

4, -1.2, 2/3, pi

String

string-length, string-repeat, string-contains?

"hello", "91"

Boolean

<, <>, <=, >=, string<?, string>?, string=?, string<>?, string>=?

true, false

Image

star, triangle, circle, square, rectangle, rhombus, ellipse, regular-polygon, radial-star, text, overlay, above, beside, rotate, scale, flip-horizontal, flip-vertical

πŸ”΅πŸ”ΊπŸ”Ά

Click here to see the prior unit-based version

Glossary
example

shows the use of a function on specific inputs and the computation the function should perform on those inputs

function

a mathematical object that consumes inputs and produces an output

function definition

code that names a function, lists its variables, and states the expression to compute when the function is used

syntax

the set of rules that defines a language, whether it be spoken, written, or programmed.

🔗There’s Got to Be a Better Way! 15 minutes

Overview

Students have already searched for structure in a list of expressions in order to define values.

In this lesson, students will build their flexibiltiy of thinking by engaging with multiple representations. Students will search for structures that are dynamic, meaning they change in a predictable way. This is the foundation for defining functions.

Launch

Students should have their workbook, pencil, and be logged into WeScheme on their computer.

I Love Green Triangles I Love Green Triangles🖼Show image

I Love Green Triangles I Love Green Triangles🖼Show image

This is a fun lesson to make silly! Dramatically confess to your students, "I LOVE green triangles!" Challenge them to use the Definitions Area to code as many unique, solid, green triangles as they can in 2 minutes.

Walk around the room and give positive feedback on the green triangles. When the time is up, ask for some examples of green triangles that they wrote and copy them to the board. Be specific and attend to precision with the syntax such that students can visually spot the pattern between the different lines of code.

For example:

(triangle 30 "solid" "green")

(triangle 12 "solid" "green")

(triangle 500 "solid" "green")

  • Is there a pattern? Yes, the code mostly stayed the same with one change each time.

  • What stayed the same? The function name triangle, "solid", "green".

  • What changed? The size of the triangle, or the Number input.

  • How many of you typed out the code from scratch each time? How many triangles were you able to code in a minute? Write this down so that you can compare to it later!!!

  • Did you know that there is a keyboard shortcut for making the previous line of code reappear in the interacions area? alt/option up-arrow

Investigate

Our programming language allows us to define values. This lets us create "shortcuts" to reuse the same code over and over.

For example: (define PRIZE-STAR (star 65 "solid" "pink"))

This code will let us write PRIZE-STAR wherever we want that same solid, pink star - without having write all the code again and again.

But to make a shortcut that changes such as creating solid, green triangles of a changing size, we need to define a function.

Suppose we want to define a shortcut function called gt. When we give it a number, it makes a solid green triangle of whatever size we give it.

Select a student to act out gt. Make it clear to the class that their Name is "gt", they expect a Number, and they will produce an Image. Act out some examples before having the class add their own and record them on the board:

  • You say: gt 20! The student responds: (triangle 20 "solid" "green")!

  • You say: gt 200! The student responds: (triangle 200 "solid" "green")!

  • You say: gt 99! The student responds: (triangle 99 "solid" "green")!

Synthesize

Thank your volunteer. Assuming they did a wonderful job, ask them:

  • How did you get to be so speedy at building green triangles? You seemed so confident! Ideally they’ll tell you that they had good instructions and that it was easy to follow the pattern

Just as we were able to give our volunteer instructions that let them take in gt 20 and give us back (triangle 20 "solid" "green"), we can define any function we’d like in the Definitions Area.

🔗Examples and Definitions

Launch

We need to program the computer to be as smart as our volunteer. But how do we do that? We already know how to do this in math!

  • Draw the table on the left below on the board.

  • We recommend starting by showing it without the equation at the bottom and talking students through the process of highlighting the variable & defining the function.

  • Once you have crowd-sourced the equation from the math side, show students how the same process of writing examples and defining the function would work in Pyret syntax.

Math WeScheme

Math Math🖼Show image

➞

WeScheme WeScheme🖼Show image

Investigate

  • Start by looking at each table and highlighting what is changing from the first row to the following rows.

  • Then, match each table to the function that defines it.

You may also want to have students complete Matching Examples & Function Definitions (Desmos)

Now that we’ve seen how this works in math, let’s go back to gt.

In the case of gt, the domain was a number and that number stood for the size of the triangle we wanted to make. Whatever number we gave gt for the size of the triangle is the number our volunteer inserted into the triangle function. Everything else stayed the same no matter what! We need to define gt in terms of the variable size, instead of in terms of a specific number.

Turn to Matching Examples and Function Definitions (Page 41) and look at the definition of gt in the first row of the table.

Using gt as a model, match the mystery function examples to their corresponding definitions.

You may also want to have students complete Matching Examples & Function Definitions (Desmos).

Connecting to Best Practices

- Writing the examples is like "showing your work" in math class.

- Have students circle what is changing and label it with a proper variable name. The name of the variable should reflect what it represents, such as size.

- Writing examples and identifying the variables lays the groundwork for writing the function, which is especially important as the functions get more complex. Don’t skip this step!

Synthesize

  • What strategies did you use to match the examples with the function definitions?

  • Why is defining functions useful to us as programmers?

🔗Examples and Contracts

Launch

  • What is the contract for triangle?

triangle :: Number, String, String -> Image

  • What is the contract for gt?

gt :: Number -> Image

  • Why might someone think the domain for gt contains a Number and two Strings? The function gt only needs one Number input because that’s the only part that’s changing. The function gt makes use of triangle, whose Domain is Number String String, but gt already knows what those strings should be.

Investigate

Confirm that everyone is on the same page before moving on. You may want to have students turn to a partner, compare their findings, and discuss their thinking about anything they didn’t agree on at first.

Have students open the gt starter file (Wescheme).

  • Click Run and evaluate (gt 10) in the Interactions Area.

  • What did you get back? a little green triangle!

  • Take one minute and see how many different green triangles you can make using the gt function.

  • Try changing one of the examples to be incorrect and click run again. What happens? The editor lets us know that the function doesn’t match the examples so that we can fix our mistake!

On the top half of the page you will see the contract, examples, and function defintion for gt. Using gt as a model, complete the contract, examples and function defintion for bc. Then type the Contract, Examples and Definition into the Definitions Area, click β€œRun”, and make sure all of the examples pass!

If you have time, have students complete

Synthesize

  • Functions can consume values besides Numbers. What other datatypes did you see being consumed by these functions?

  • Thumbs up? Thumbs to the side? or Thumbs down? How confident do you feel that you could write the contract, examples and function definition on your own if you were given a word problem about another shape function?

🔗Additional Exercises:

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). CCbadge Bootstrap:Algebra by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.