instagram

Students discover functions as an abstraction over a programming pattern, and are introduced to a structured approach to building them called the Design Recipe.

Prerequisites

Relevant Standards

Select one or more standards from the menu on the left (⌘-click on Mac, Ctrl-click elsewhere).

Common Core Math Standards
6.EE.B.6

Use variables to represent numbers and write expressions when solving a real-world or mathematical problem; understand that a variable can represent an unknown number, or, depending on the purpose at hand, any number in a specified set.

7.EE.B.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.B

Use functions to model relationships between quantities.

8.SP.A.1

Construct and interpret scatter plots for bivariate measurement data to investigate patterns of association between two quantities. Describe patterns such as clustering, outliers, positive or negative association, linear association, and nonlinear association.

HSA.SSE.A.1

Interpret expressions that represent a quantity in terms of its context.

HSF.BF.A.1

Write a function that describes a relationship between two quantities.

HSF.IF.A

Understand the concept of a function and use function notation.

HSF.IF.A.2

Use function notation, evaluate functions for inputs in their domains, and interpret statements that use function notation in terms of a context.

HSF.IF.B

Interpret functions that arise in applications in terms of the context.

HSF.IF.C

Analyze functions using different representations.

MP.4

Model with mathematics

CSTA Standards
1B-AP-15

Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended.

2-AP-11

Create clearly named variables that represent different data types and perform operations on their values.

2-AP-14

Create procedures with parameters to organize code and make it easier to reuse.

2-AP-17

Systematically test and refine programs using a range of test cases

2-AP-19

Document programs in order to make them easier to follow, test, and debug.

3B-AP-14

Construct solutions to problems using student-created components, such as procedures, modules and/or objects.

3B-AP-21

Develop and use a series of test cases to verify that a program performs according to its design specifications.

K-12CS Standards
6-8.Algorithms and Programming.Modularity

Programs use procedures to organize code, hide implementation details, and make code easier to reuse. Procedures can be repurposed in new programs. Defining parameters for procedures can generalize behavior and increase reusability.

6-8.Algorithms and Programming.Variables

Programmers create variables to store data values of selected types. A meaningful identifier is assigned to each variable to access and perform operations on the value by name. Variables enable the flexibility to represent different situations, process different sets of data, and produce varying outputs.

9-12.Algorithms and Programming.Modularity

Complex programs are designed as systems of interacting modules, each with a specific role, coordinating for a common overall purpose. These modules can be procedures within a program; combinations of data and procedures; or independent, but interrelated, programs. Modules allow for better management of complex tasks.

P4

Developing and Using Abstractions

Next-Gen Science Standards
HS-SEP5-3

Apply techniques of algebra and functions to represent and solve scientific and engineering problems.

Oklahoma Standards
OK.8.AP.PD.02

Incorporate existing code, media, and libraries into original programs of increasing complexity and give attribution.

OK.A1.A.1.1

Use knowledge of solving equations with rational values to represent and solve mathematical and real-world problems (e.g., angle measures, geometric formulas, science, or statistics) and interpret the solutions in the original context.

OK.A1.F.1.3

Write linear functions, using function notation, to model real-world and mathematical situations.

OK.PA.A.1.1

Recognize that a function is a relationship between an independent variable and a dependent variable in which the value of the independent variable determines the value of the dependent variable.

OK.PA.A.1.2

Use linear functions to represent and explain real-world and mathematical situations.

Lesson Goals

Students will be able to:

  • describe the usefulness of functions.

  • create their own functions and examples given the constraints of a problem.

  • define one-argument functions that consume a Number and produce an Image

  • define one-argument functions that consume a Number and produce a Number

  • define one-argument functions that consume a String and produce an Image

Student-Facing Lesson Goals

  • I can explain why a function is useful.

  • I can write my own function in Pyret.

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

num-sqrt

4, -1.2, 2/3, pi

String

string-length, string-repeat, string-contains

"hello", "91"

Boolean

<, <>, <=, >=, <, >, ==, <>, >=

true, false

Image

star, triangle, circle, square, rhombus, ellipse, regular-polygon, radial-star, bar-chart, pie-chart, pie-chart-summarized

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

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

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 code.pyret.org 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? up-arrow

Investigate

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 Pyret

400 400🖼Show image

➞

400 400🖼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 31) 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 (Pyret) .

  • 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:Data Science 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.