Lesson 14: Songwriting

Overview

One of the most magnificent structures in the computer science world is the function. Functions (sometimes called procedures) are mini programs that you can use over and over inside of your bigger program. This lesson will help students intuitively understand why combining chunks of code into functions can be such a helpful practice.

Purpose

The use of functions helps simplify code and develop the student's ability to organize their program. Students will quickly recognize that writing functions can make their long programs easier to read and easier to debug if something goes wrong.

Agenda

Warm Up (20 min)

Main Activity (20 min)

Wrap Up (5 min)

Assessment (5 min)

Extended Learning

View on Code Studio

Objectives

Students will be able to:

  • Locate repeating phrases inside song lyrics.
  • Identify sections of a song to pull into a function.
  • Describe how functions can make programs easier to write.

Preparation

  • (Optional) Watch the Lesson in Action Video.
  • Print several worksheets for each group.
  • Print one assessment for each student.
  • Secure access to songs and lyrics for activity.
  • Make sure every student has a journal.

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the Teachers

For the Students

Vocabulary

  • Function - A piece of code that you can easily call over and over again.

Support

Report a Bug

Teaching Guide

Warm Up (20 min)

Vocabulary

This lesson has one new and important word:

  • Function - Say it with me: Func-shun

A piece of code that you can call over and over again.

Sing a Song

  • Let the class know that today is song day!
  • We're going to learn a song together.
    • Start with a simple song, either written out or projected on the screen.
    • Point to the chorus and be sure that the class knows how it goes before you begin on the song.
    • Blast through the song, singing it with them in the beginning, then see what happens when you get to the part where it calls the chorus.

Teaching Tip

Little Bunny Foo Foo is being used here as an example only. If your students know this song, feel free to use it. Otherwise, choose an appropriate song that they might be more familiar with (either from music class or the radio.)

Chorus:

Little bunny Foo Foo
Hopping through the forest
Scooping up the field mice
And bopping ‘em on the head
Down came the Fairy
And she said
“Little bunny Foo Foo
I don’t wanna see you
Scooping up the field mice
And bopping ‘em on the head”*

Song:

Chorus

I’ll give you 3 chances.
Then I’ll turn you into a goon!
The next day. . .

Chorus

I’ll give you 2 chances.
Then I’ll turn you into a goon!
The next day. . .

Chorus

I’ll give you 1 chance.
Then I’ll turn you into a goon!
The next day. . .

Chorus

"I gave you two chances.
Now I’ll turn you into a goon!”
(POOF!)
And the moral of the story is:
Hare today, goon tomorrow!

  • It's quite likely that the majority of the class will sing the lyrics for the chorus when you point to that bit.
    • Stop the song once that happens, and explicitly highlight what just happened.
      • You defined the chorus.
      • You called the chorus.
      • They sang the chorus.
  • Ask the class why they suppose you only wrote the chorus once at the top of the paper instead of writing it over and over in each place where it is supposed to be sung.
    • What are other benefits of only writing the chorus once when you sing it many times?

Lesson Tip

To hit this point home, you can look up the lyrics for some popular songs on the Internet. Show the students that the standard for repeating lyrics is to define the chorus at the top and call it from within the body of the song.

Now, imagine that this song is a computer program. Defining a title (like "chorus") for a little piece of code that you use over and over again is called creating a function.

This is helpful to computer scientists for some of the same reasons that it is helpful to songwriters.

  • It saves time not having to write all the code over and over in the program.
  • If you make a mistake, you only have to change it one place.
  • The program feels less complicated with the repeating pieces defined just once at the top.

We are going to play with songs a little more, to try to really understand how often this technique is used!

Main Activity (20 min)

Functions Unplugged: Songwriting - Worksheet

A fantastic way to compare functions to something we see in our everyday lives is to look at songs. Songs often have certain groups of lyrics that repeat over and over. We call such a group a "chorus."

Directions:

Lesson Tip

It's most exciting for students to do this lesson with popular music from the radio, but if you're having a hard time finding appropriate songs where the lyrics repeat exactly, here are a few timeless options:

  • Divide into groups of 4, 5, or 6.
  • Give each group several copies of the Songwriting Worksheet.
  • Play a short song for the class that contains a clear chorus that does not change from verse to verse.
  • Challenge the class to identify (and write down) the chorus.
  • Compare results from each group.

Did everyone get the same thing? Sing your choruses together to find out! Play this game over and over until the class has little trouble identifying the choruses.

  • It is often easier just to have the class listen to (or watch) the song, then vote on what the chorus is by singing it together, rather than writing the whole thing down. If you choose this method, consider having the class do a written chorus for the final song selection to be sure that the visual learners get proper reinforcement.

Wrap Up (5 min)

Flash Chat: What did we learn?

Lesson Tip

Flash Chat questions are intended to spark big-picture thinking about how the lesson relates to the greater world and the students' greater future. Use your knowledge of your classroom to decide if you want to discuss these as a class, in groups, or with an elbow partner.

  • Would you rather write lyrics over and over again or define a chorus?
  • Do you think it's possible to make multiple choruses for the same song?
  • Does it make sense to make a new chorus for every time it's needed in a song?

Journaling

Having students write about what they learned, why it’s useful, and how they feel about it can help solidify any knowledge they obtained today and build a review sheet for them to look to in the future.

Journal Prompts:

  • What was today's lesson about?
  • How do you feel about today's lesson?
  • What is a function and how do you use it?
  • Can you think of another activity where you might want to call a special group of instructions several times?

Assessment (5 min)

Functions Unplugged: Songwriting - Assessment

Hand out the assessment worksheet and allow students to complete the activity independently after the instructions have been well explained. This should feel familiar, thanks to the previous activities.

Extended Learning

Use these activities to enhance student learning. They can be used as outside of class activities or other enrichment.

Functional Suncatchers Visit the CS Fundamentals Unplugged Table or click on the link for Functional Suncatchers. This activity does take a few supplies from the craft store, but it helps students to see the value of calling multiple functions.

Create Your Song

  • Start by creating a chorus together, then repeat it between verses of a song that you develop around it.
  • Make a change to the chorus, and ponder how much easier it is to change in just one place.
  • Change the chorus again, making it much longer than it was originally.
  • Add a second chorus and alternate between them in your verses.

Songwriting a Program

  • What if we acted out songs instead of singing them? All of a sudden, our chorus would be a function of repeated actions, rather than words.
  • Use the concepts of the arrows from the Graph Paper Programming lesson and create a program with lots of repeating instructions.
    • Circle those repeating actions so that the class can see where they are.
    • Define a function called "Chorus" above the program.
    • Cross out everywhere the repeating actions appear in the program and write "Chorus" instead.
  • Repeat until the class can go through this process with little direction.
  • Practice
  • 1
  • 2
  • (click tabs to see student view)
View on Code Studio

Student Instructions

"This is going to BEE great!"

Help the bee collect all of the nectar.

View on Code Studio

Student Instructions

"This is going to BEE great!"

Help the bee collect all of the nectar.

  • Nested Loops with the Bee
  • 3
  • (click tabs to see student view)
  • Prediction
  • 4
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Look closely at the nested loops below. What will happen when you click "Run"?

The bee will move forward and get nectar only one time.

The bee will get only 2 units of nectar.

The bee will get all of the nectar.

I don't know.

View on Code Studio

Student Instructions

This time, help the bee collect all of the nectar using as few blocks as possible.

View on Code Studio

Student Instructions

"Zombie hungry!"

Get the zombie to the sunflower using the fewest number of blocks possible.

View on Code Studio

Student Instructions

Get the zombie to the sunflower using the fewest blocks possible!

View on Code Studio

Student Instructions

View on Code Studio

Student Instructions

"Must eat sunflower!"

Get the zombie to the sunflower using only the blocks available.

  • Challenge
  • 10
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Challenge: Figure out how to get all of the nectar using only the blocks available.

  • Practice
  • 11
  • 12
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Collect all of the nectar from each flower and make honey at the honeycomb.

View on Code Studio

Student Instructions

Make all of the honey.

  • Prediction
  • 13
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Look closely at the code below. How much honey will the bee make when you click "Run"?

4

8

12

I don't know.

View on Code Studio

Student Instructions

"I need some help on the farm! Help me harvest this corn using only the blocks in your toolbox."

View on Code Studio

Student Instructions

"Let's hunt for treasure - using loops!"

Help the collector reach at least two piles of treasure using only the blocks in your toolbox. It is possible to collect all 4.

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 1B-AP-08 - Compare and refine multiple algorithms for the same task and determine which is the most appropriate.
  • 1B-AP-11 - Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process.

Cross-curricular Opportunities

This list represents opportunities in this lesson to support standards in other content areas.

Common Core English Language Arts Standards

L - Language
  • 4.L.6 - Acquire and use accurately grade-appropriate general academic and domain-specific words and phrases, including those that signal precise actions, emotions, or states of being (e.g., quizzed, whined, stammered) and that are basic to a particular topic (e.g
SL - Speaking & Listening
  • 4.SL.1 - Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 4 topics and texts, building on others’ ideas and expressing their own clearly.
  • 4.SL.4 - Report on a topic or text, tell a story, or recount an experience in an organized manner, using appropriate facts and relevant, descriptive details to support main ideas or themes; speak clearly at an understandable pace.

Common Core Math Standards

MP - Math Practices
  • MP.1 - Make sense of problems and persevere in solving them
  • MP.2 - Reason abstractly and quantitatively
  • 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

Next Generation Science Standards

ETS - Engineering in the Sciences
ETS1 - Engineering Design
  • 3-5-ETS1-1 - Define a simple design problem reflecting a need or a want that includes specified criteria for success and constraints on materials, time, or cost.
  • 3-5-ETS1-2 - Generate and compare multiple possible solutions to a problem based on how well each is likely to meet the criteria and constraints of the problem.
  • 3-5-ETS1-3 - Plan and carry out fair tests in which variables are controlled and failure points are considered to identify aspects of a model or prototype that can be improved.