instagram

Students learn how to define names for computed values. These names can be used repeatedly in different situations, jusr as they are in variable in math.

Prerequisites

None

Relevant Standards

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

Common Core Math Standards
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

CSTA Standards
2-AP-14

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

Oklahoma Standards
OK.PA.A.3.1

Use substitution to simplify and evaluate algebraic expressions.

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 names for values, generated by computed expressions.

  • I can use my defined values in different situations.

Materials

Preparation

  • Make sure all materials have been gathered

  • Decide how students will be grouped in pairs

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.

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
contract

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

data types

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

definitions area

the left-most text box in the Editor where definitions for values and functions are written

value

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

variable

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

🔗What’s in Common? 30 minutes

Overview

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

Launch

Take a look at the expressions below:

(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 code do they all have in common? (star 50 "solid" "green")

  • What would happen if you were asked to change the color of all the stars to gold? We’d have to change it everywhere it appeared.

Duplicate code is almost always bad!

There are lots of potential problems with duplicate code:

  • Readability: The more code there is, the harder it can be to read.

  • Performance: Why re-evaluate the same code a dozen times, when we can evaluate it once and use the result as many times as we need?

  • Maintainability: Suppose we needed to change the size of the stars in the examples above. We would have to make sure every line is changed, which leaves a lot of room for error.

Since we’re using that star over and over again, wouldn’t it be nice if we could define a "nickname" for that code, and then use the nickname over and over in place of the expression? And then, if we wanted to change something about all of the stars, we would only have to make the change once, in the definition.

You already know how to do this in math:

x = 4 defines the nickname x to be the value 4.

WeScheme uses the word "define" to make this even clearer!

We can type (define x (4)) to define x to be the value 4.

Once we’ve defined x to be the value 4 and clicked "run", anytime we use x, the computer will remember that it is defined as 4. We can use that definition to get an answer. For example, x + 2 will evaluate to 6.

Of course, the whole point of defining a value is so that it sticks around and can be used later! That’s why programmers put their definitions on the left-hand side, known as the Definitions Area.

Investigate

Synthesize

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

  • What values did you decide to define? When might they be useful?

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".

🔗Using Defined Values

Overview

Now that we know how to define values, we’ve got two more things to consider:

  • When it would be useful to define them

  • How to use them once we have

Launch

Have students open to Defining Values - Chinese Flag (Page 35). It will direct them to open the Chinese flag Starter File (Wescheme)

once they complete the first half of the questions on the page.

Investigate

  • Have students open the editor and type (radial-star 30 20 50 "solid" "yellow") into the interactions area and click run.

  • Have students work in the Definitions area to define a value sun to be the image (radial-star 30 20 50 "solid" "yellow").

  • Turn to Why Define Values? (Page 36). The first row of the table has been completed for you. Could I get a volunteer to explain what they see happening in that first row?

  • Have students complete the page and test their code in the editor.

Synthesize

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

Optional: Fun with Flags! Now that students have learned how to define values, you may want to give them an opportunity to practice. Our Flags lesson dives deeper into image composition and includes some starter files specifically focused on cleaning up code by defining values.

🔗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.