Lesson 5: Defining Variables and Substitution
In this activity, students will learn to define variables that can be used to reference values and expressions. Once defined, their variables can be used repeatedly throughout a program as substitutes for the original values or expressions.
As stated earlier, the programming paradigm that students are learning in this course is called functional programming, and it varies in several ways from programming paradigms that students may have seen elsewhere. One of the core tenants of functional programing is the concept of immutability. Immutable objects may not change state once they have been created, which allows variables to functional as we would expect them to in Algebra.
Defining Variables and Substitution
Common Core Math Standards
- 6.EE.4 - Identify when two expressions are equivalent (i.e., when the two expressions name the same number regardless of which value is substituted into them). For example, the expressions y + y + y and 3y are equivalent because they name the same number regardless of which number y stands for.
Students will be able to:
- Define variables by giving them a name and assigning them a value or expression.
- Use variables within Evaluation Blocks.
- Describe a situation where using variables as substitutions for values or expressions is more efficient.
- Define - Associate a descriptive name with a value.
- Variable - A reference to a value or expression that can be used repeatedly throughout a program.
Even worse, if you decided you wanted seventy-five blue triangles instead, you'd have to go through and change each and every block. There must be a better way!
We can store that red triangle Evaluation Block in a Variable, let's call it "red-triangle." That name "red-triangle" now becomes a shortcut for the blocks inside the variable, and we can use that shortcut over and over in our program. If we decide that we want that red triangle to be 100 pixels instead of 50, we only need to change it in the variable definition.
If students have used variables in other programming languages, it's essential to note that in functional programming, as in math, variables are considered immutable - meaning the value can't be changed during the execution of a program. Think about it this way: saying
x = 50, and then
Defining Variables and Substitution
In this stage you'll use variables to reference a variety of values and expressions. Head to Course A stage 5 in Code Studio to get started programming.
Variables allow us to name values so that we can easily refer to them repeatedly throughout our programs. Here's a variable called
age. Set its value to your age and click run.
Here we've set the
age variable to 17 years. Can you write an expression that calculates
age in days?
Can you write an expression that calculates
age in months?
Here's a new variable called
age-in-months. Use the variable
age to set
Write a program that produces a 50 pixel solid green triangle.
If we wanted to make fifty green triangles, it would be a pain to repeat that block of code 50 times. Let's create a variable called
my-shape and put the code for a 50 pixel solid green triangle inside.
my-shape variable and use it with this provided code to draw two identical green triangles that are 50 pixels in size.
my-shape variable so that it draws 75 pixel solid red stars.
Free Play: We've provided a function that fills your screen with
my-image to see what patterns you can make.
Common Core Math Standards
CED - Creating Equations
- A.CED.1 - Create equations and inequalities in one variable and use them to solve problems. Include equations arising from linear and quadratic functions, and simple rational and exponential functions.
- A.CED.2 - Create equations in two or more variables to represent relationships between quantities; graph equations on coordinate axes with labels and scales.
EE - Expressions And Equations
- 6.EE.4 - Identify when two expressions are equivalent (i.e., when the two expressions name the same number regardless of which value is substituted into them). For example, the expressions y + y + y and 3y are equivalent because they name the same number regardles
IF - Interpreting Functions
- F.IF.1 - Understand that a function from one set (called the domain) to another set (called the range) assigns to each element of the domain exactly one element of the range. If f is a function and x is an element of its domain, then f(x) denotes the output of f c
- F.IF.2 - Use function notation, evaluate functions for inputs in their domains, and interpret statements that use function notation in terms of a context.
- F.IF.3 - Recognize that sequences are functions, sometimes defined recursively, whose domain is a subset of the integers. For example, the Fibonacci sequence is defined recursively by f(0) = f(1) = 1, f(n+1) = f(n) + f(n-1) for n ≥ 1.
LE - Linear, Quadratic, And Exponential Models★
- F.LE.1 - Distinguish between situations that can be modeled with linear functions and with exponential functions.
MP - Math Practices
- MP.1 - Make sense of problems and persevere in solving them
- MP.2 - Reason abstractly and quantitatively
- MP.3 - Construct viable arguments and critique the reasoning of others
- MP.4 - Model with mathematics
- MP.5 - Use appropriate tools strategically
- MP.6 - Attend to precision
- MP.7 - Look for and make use of structure
- MP.8 - Look for and express regularity in repeated reasoning
OA - Operations And Algebraic Thinking
- 5.OA.1 - Use parentheses, brackets, or braces in numerical expressions, and evaluate expressions with these symbols.
- 5.OA.2 - Write simple expressions that record calculations with numbers, and interpret numerical expressions without evaluating them. For example, express the calculation “add 8 and 7, then multiply by 2” as 2 × (8 + 7). Recognize that 3 × (18932 + 921) is three t
Q - Quantities
- N.Q.1 - Use units as a way to understand problems and to guide the solution of multi-step problems; choose and interpret units consistently in formulas; choose and interpret the scale and the origin in graphs and data displays.
- N.Q.2 - Define appropriate quantities for the purpose of descriptive modeling.
SSE - Seeing Structure In Expressions
- A.SSE.1 - Interpret expressions that represent a quantity in terms of its context.
- A.SSE.2 - Use the structure of an expression to identify ways to rewrite it. For example, see x4 – y4 as (x2)2 – (y2)2, thus recognizing it as a difference of squares that can be factored as (x2 – y2)(x2 + y2).