instagram

Students learn how to define lines of code as a set value that can be used repeatedly in different situations, similar to a variable in math.

Prerequisites

Relevant Standards

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

Common Core State Statements
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.

HSA.SSE.A.1

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

MP.2

Reason abstractly and quantitatively

MP.7

Look for and make use of structure

Older Statements
BS-PL.3

The student is able to use the syntax of the programming language to define values and functions

Lesson Goals

Students will be able to:

  • Demonstrate understanding of variables and why they are useful in math and programming

Student-facing Goals

  • I can define a line of code as a value, such as myStar or eyeColor.

  • I can use my defined value in different situations.

Materials

Preparation

  • Make sure all materials have been gathered

  • Decide how students will be grouped in pairs

Supplemental Resources

Key Points For The Facilitator

  • Learning how to define values is a big milestone! It will be used consistently throughout other lessons, so be sure to give students plenty of time to practice this new skill.

  • Check frequently for understanding of data types and contracts during this lesson and throughout subsequent lessons.

  • Students will use their Contracts page frequently, so it should be kept in an accessible, convenient location.

Glossary
contract

a statement of the name, domain, and range of a function

datatypes

a way of classifying values, such as: Number, String, Image, Boolean, or any user-defined data structure

function

a mathematical object that consumes inputs and produces an output

value

a specific piece of data, like 5 or "hello"

variable

a letter, symbol, or term that stands in for a value or expression

Warmup

Students should be logged into WeScheme.

Identifying Fixed Structures 30 minutes

Overview

This activity introduces the problem with duplicate code, leveraging Mathematical Practice 7 - Identify and Make Use of Structure. Students identify common structures in an expression, and discover ways to bind that expression to a name that can be re-used.

Launch

Display the sample code from the "Prize Star" exercise.

(star 50 "solid" "green")
(scale 3 (star 50 "solid" "green"))
(scale .5 (star 50 "solid" "green"))
(rotate 45 (star 50 "solid" "green"))
(rotate 45 (scale 3 (star 50 "solid" "green")))
  • What common element was repeated throughout all the lines of code? (star 50 "solid" "green")

  • What happened when you were asked to change the color of the star to gold? We had to change it everywhere it appeared.

Since we’re using that star over and over again, wouldn’t it be nice if we could give the piece of code a "nickname", something shorter we could type to represent that piece of code?

Investigate

In the coding environment, have students click "Start a new program" and open to Defining Values - Explore (Page 21) to explore defining values.

  • How is ‘define’ similar to a function? How is it different? It’s written inside parentheses like a function, but ‘define’ doesn’t have an output

  • What is the usefulness of defining values? Lets the programmer reuse code, saves time, lets the programmer make changes easily, allows us to more easily use elements inside other functions

  • What datatypes can we define values for? All of them - Number, String, Image

Support for English Language Learners

MLR 8 - Discussion Supports: As students discuss, rephrase responses as questions and encourage precision in the words being used to reinforce the meanings behind some of the programming-specific language, such as "define" and "value".

Students complete Defining Values Practice 2 (Page 22) with their partner.

Cleaning Up 20 minutes

Overview

This activity is a chance to play with new concepts, combining value definitions and function composition to create new shapes or to clean up code that generates shapes. The engaging nature of the activity is designed to motivate lots of experiments, each of which gives students a chance to practice applying those concepts.

Launch

The ability to define values allows us to look for - and make use of - structure in our code or in our equations. What structures are repeated in this expression?

Investigate

Have students open this file , which draws the Chinese flag.

  1. Have them change the color of all the star from yellow to black

  2. Have them identify what structures are repeated

  3. Have them use a value definition to simplify the code

  4. Have them change the stars from black back to yellow

Optional (for a longer time commitment): Have students choose a flag from this list: (Flags of the World Resource), and recreate one (or more!) of the flags using define and any of the other functions they’ve learned so far.

Synthesize

How many reasons can students come up with for why defining values is useful?

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). CCbadge BS:Games by Jen Poole, Emmanuel Schanzer, Ed Campos Jr, and Dorai Sitaram is licensed under a Creative Commons 4.0 Unported License. Based on a work at www.BootstrapWorld.org. Permissions beyond the scope of this license may be available by contacting schanzer@BootstrapWorld.org.