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.


Relevant Standards

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

Oklahoma Standards

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 a line of code as a value, such as myStar or eyeColor.

  • I can use my defined value in different situations.



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

Click here to see the prior unit-based version.


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


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


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


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

🔗What’s in Common? 30 minutes


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.


Students should be logged into

Take a look at the expressions below:

star(50, "solid", "green")
scale(3, star(50, "solid", "green"))
scale(0.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?


You already know how to do this in math: x = 4 + 2 evaluates the expression, and defines the nickname x to be the value 6.

Pyret is no different! We type x = 4 + 2 to define x to be the value 6.

  • Start a new program, and type this code into the Interactions Area.

  • What happens when you hit Enter?

  • Can you explain what happened or didn’t happen?

Expressions evaluate to answers. Definitions don’t.

Think back to math: x = 4 + 2 doesn’t have an "answer". All it does is tell us that anytime we see x, we know it stands for 6. We only see a result when we use that definition, for example x × 5 will evaluate to 30.

On the computer, try using the definition of x by multiplying it by 5.

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

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.

🔗Cleaning Up Code 20 minutes


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.


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

(x + 1)^2 - 4/( (x + 1) ) × -2(x + 1)


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

  1. This file uses a function students haven’t seen before! What is it?

  2. What is it its contract?

  3. Have them change the color of all the stars from yellow to black

  4. Have them identify what structure is repeated

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

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

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


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 Bootstrap:Integrated Oklahoma by Jen Poole is licensed under a Creative Commons 4.0 Unported License. Based on a work at Permissions beyond the scope of this license may be available by contacting