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
Older Statements
|
Lesson Goals |
Students will be able to:
|
Student-facing Goals |
|
Materials |
|
Preparation |
|
Supplemental Resources |
|
Key Points For The Facilitator |
|
- 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.
-
Have them change the color of all the star from yellow to black
-
Have them identify what structures are repeated
-
Have them use a value definition to simplify the code
-
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).
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.