Lesson 9: Functions Explore / Investigate
App Lab
Overview
Students begin the lesson by considering two ways to write out the lyrics of a song, one that includes a lot of repeated text and one that does not. After exploring this example students complete a series of investigate activities in which functions have been used to remove repeated code from a program. At the conclusion of the lesson students discuss the concept of a function to synthesize their learning and add definitions to their journal.
Purpose
This lesson is both the Explore and Investigate components of the EIPM sequence for functions. Functions are best understood by actually using them in a program, and so while the Explore component is relatively shorter, students actually are given many opportunities to observe how functions are used in programs. There is a heavy emphasis on running programs slowly to see how functions change the order in which programs run.
If you are a teacher with more experience with the concept of functions you may be wondering where other related concepts like parameters, arguments, and return values, will be introduced. In this unit these additional concepts are not learning objectives, and they will not be discussed in detail until later units. For now, it is fine for students to think of functions simply as a way to name a collection of commands so that they can be used in multiple places within your code.
Agenda
Lesson Modifications
Warm Up (5 mins)
Activity (30 mins)
Wrap Up (10 mins)
View on Code Studio
Objectives
Students will be able to:
- Use appropriate vocabulary to describe the declaring and calling of functions
- Trace the flow of execution in programs that declare and call functions
- Describe the way a function call interrupts the normal flow of execution within a program
- Modify programs that declare and call functions to adjust their functionality
Preparation
- Print copies of the activity guide of song lyrics used in the warm up or determine how you'll project it to the class.
- Review the apps that students will investigate in the app
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the Teachers
- CSP Unit 4 - Variables, Conditionals, and Functions - Presentation
For the Students
- Song Lyrics - Activity Guide
Teaching Guide
Lesson Modifications
Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, please read the full lesson plan below, then click here to access the modifications.
Warm Up (5 mins)
Explore Song Lyrics
Discussion Goal
Goal: This lesson does not feature a large hands-on explore segment, but students should still get a quick introduction to the concept of a function before jumping in the code to try it out. Run this discussion to help motivate the ideas underlying functions. Here are the main points to bring out.
- In Style 1 you read line 10 after line 09. This is a the normal order we're used to.
- In Style 2 you sing the lyrics “out of order”. After line 09 you’re going to sing line 35
- It’s shorter to write the song but it’s the same song and will take the same time / lyrics to sing.
- Style 2 removes repetition and it also makes it a little easier to understand the overall structure of the song
Distribute: Share print copies of Song Lyrics - Activity Guide or project the activity guide where all can see it.
- In Style 1, what line of the song do you sing after line 09? What about in Style 2?
- Style 2 uses fewer lines to write. Are there fewer lyrics to sing?
- What are the benefits of writing a song in Style 2?
Remarks
This second song is written in a style that we're going to use to write some of our programs. In fact, we're going to start off by looking at a program that "sings" this exact same song.
Activity (30 mins)
Investigate
Group: Group students in pairs.
Do This: Direct students to Level 2 for the lesson on Code Studio.
Level 2 - Explore Song Lyrics: This level is a continuation of the Explore activity in the warmup. Students should watch the code run slowly to see how the code that is running will jump down to the bottom of the program (the function declaration) when the name of the function is used (the function call).
Remarks
This concept that we just explored in both text and a program is called a "function". We're going to watch a short video that explains it in more detail.
Level 3 - Video: Watch the video as a class. The most important three takeaways are that
- Functions are declared in your program in order to give a group of commands a name
- Functions are called to run those commands.
- A function is only declared once but is called as many times as you wish.
Level 4 - Investigate Score Clicker
- Discuss: Once students have had a chance to run the code ask them to explore it and be ready to share responses to the questions provided.
- The function is declared on line 23 but called three times, on lines 6, 12, and 19.
- This way of writing the program makes the code in each of the buttons much simpler to read. It also removes repeated code.
- Modify: Have students modify the program as instructed. All of the modifications should be made from within the function. Afterwards call out the following points.
- If you change the function you can use those changes every place the function is called.
- These changes make it easier to debug your program since you are not writing redundant code. You only need to rewrite and debug the code once.
Level 5 - Investigate Lemon Squeeze App with Functions
- Modify: Students in this level are asked to create a function themselves by identifying repeated code. They'll need to create a single function for this repeated code.
- Discuss: After creating their function lead a short discussion on the benefits of creating a function to remove repeated code.
Wrap Up (10 mins)
Synthesizing Discussion
Journal: Review the updateScreen pattern with students. Afterwards have them add to their journal definition for a function and a function call.
Discussion Goal
Goal: Use this quick discussion to identify any open questions that students have at the conclusion of the lesson. These can help you know where to focus attention in the next lesson.
Prompt: Reflecting on today's lesson about functions:
- What did you learn?
- What are you uncertain about?
Assessment: Check For Understanding
Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.
Question: In your own words describe the benefits of creating functions in your code.
Standards Alignment
View full course alignment
CSTA K-12 Computer Science Standards (2017)
AP - Algorithms & Programming
- 3A-AP-17 - Decompose problems into smaller components through systematic analysis, using constructs such as procedures, modules, and/or objects.
- 3B-AP-23 - Evaluate key qualities of a program through a process such as a code review.
CSP2021
AAP-3 - Programmers break down problems into smaller and more manageable pieces
AAP-3.A - For procedure calls: a. Write statements to call procedures. b. Determine the result or effect of a procedure call.`
- AAP-3.A.1 - A procedure is a named group of programming instructions that may have parameters and return values.
- AAP-3.A.2 - Procedures are referred to by different names, such as method or function, depending on the programming language.
- AAP-3.A.4 - A procedure call interrupts the sequential execution of statements, causing the program to execute the statements within the procedure before continuing. Once the last statement in the procedure (or a return statement) has executed, flow of control is ret