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 is such a helpful practice.
Getting Started - 20 minutes
Activity: Songwriting - 20 minutes
Wrap-up - 5 minutes
Assessment - 5 minutes
- Locate repeating phrases inside song lyrics
- Identify sections of a song to pull into a function (chorus)
- Describe how functions can make programs easier to write
Materials, Resources and Prep
For the Student
- Songwriting Worksheet
- Pens & Pencils
For the Teacher
Getting Started (20 min)
This is a great time to review the last lesson that you went through with your class. You can do this as one large group or have students discuss with an elbow partner.
Here are some questions that you can ask in review:
What did we do last time?
What do you wish we had had a chance to do?
Did you think of any questions after the lesson that you want to ask?
What was your favorite part of the last lesson?
Finishing the review by asking about the students' favorite things helps to leave a positive impression of the previous exercise, increasing excitement for the activity that you are about to introduce.
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
- 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.
- 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.
- Stop the song once that happens, and explicitly highlight what just happened.
- 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?
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 the 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!
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.
Activities: (20 min)
- 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."
1) Divide into groups of 4, 5, or 6.
2) Give each group several copies of the Songwriting Worksheet.
3) Play a short song for the class that contains a clear chorus that does not change from verse to verse.
4) Challenge the class to identify (and write down) the chorus.
5) 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.
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:
Wrap-up (5 min)
- 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?
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.
- Which one of these definitions did we learn a word for today?
"A piece of code that you can call over and over again"
"A baby horse"
"Getting help from a large group of people to finish something faster"
...and what is the word that we learned?
Assessment (5 min)
- 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.
Use these activities to enhance student learning. They can be used as outside of class activities or other enrichment.
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.
- Functional Suncatchers This activity shows functions from a different perspective. The students will follow a series of repetitive steps, then be asked to identify certain sets of “skills” that are duplicated several times. Once those skills are defined, they will be called from a main program and the whole beautiful process of creation will be recorded on a single sheet of paper.
Connections and Background Information
ISTE Standards (formerly NETS)
- 1 a - Apply existing knowledge to generate new ideas, products, or processes.
- 1.c - Use models and simulation to explore complex systems and issues.
- 2.d - Contribute to project teams to produce original works or solve problems.
- 4.b - Plan and manage activities to develop a solution or complete a project.
- 4.d - Use multiple processes and diverse perspectives to explore alternative solutions.
CSTA K-12 Computer Science Standards
- CPP.L1:6.05. Construct a program as a set of step-by-step instructions to be acted out.
- CT.L2-01. Use the basic steps in algorithmic problem solving to design solutions.
- CT.L2-06. Describe and analyze a sequence of instructions being followed.
- CT.L2-07. Represent data in a variety of ways: text, sounds, pictures, numbers.
- CT.L2-08. Use visual representations of problem states, structures, and data.
- CT.L2-12. Use abstraction to decompose a problem into sub-problems.
- CT.L3A-3. Explain how sequence, selection, iteration, and recursion are building blocks of algorithms.
- CT.L2-14. Examine connections between elements of mathematics and computer science including binary numbers, logic, sets, and functions.
Next-Gen Science Standards
- 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.
Common Core Mathematical Practices
- 1. Make sense of problems and persevere in solving them.
- 2. Reason abstractly and quantitatively.
- 5. Use appropriate tools strategically.
- 6. Attend to precision.
- 7. Look for and make use of structure.
- 8. Look for and express regularity in repeated reasoning.
Common Core Language Arts Standards
- SL.3.1 - Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 3 topics and texts, building on others' ideas and expressing their own clearly.
- SL.3.3 - Ask and answer questions about information from a speaker, offering appropriate elaboration and detail.
- L.3.6 - Acquire and use accurately grade-appropriate conversational, general academic, and domain-specific words and phrases, including those that signal spatial and temporal relationships.
- SL.4.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.
- L.4.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 and that are basic to a particular topic.
- SL.5.1 - Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 5 topics and texts, building on others' ideas and expressing their own clearly.
- L.5.6 - Acquire and use accurately grade-appropriate general academic and domain-specific words and phrases, including those that signal contrast, addition, and other logical relationships.