Variables, Conditionals, and Functions - Lesson 9: Functions Explore / Investigate
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.
Goals
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
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.
Resources
Getting Started (5 minutes)
(Optional) Distribute: Share the Song Lyrics - Activity Guide by giving paper copies, providing students the link, or projecting the activity guide where all can see it.
Style 1 | Style 2 |
---|---|
00 Feeling my way through the darkness 01 Guided by a beating heart 02 I can't tell where the journey will end 03 But I know where to start 04 05 They tell me I'm too young to understand 06 They say I'm caught up in a dream 07 Well life will pass me by if I don't open up my eyes 08 Well that's fine by me 09 10 So wake me up when it's all over 11 When I'm wiser and I'm older 12 All this time I was finding myself 13 And I didn't know? I was lost 14 15 So wake me up when it's all over 16 When I'm wiser and I'm older 17 All this time I was finding myself 18 And I didn't know? I was lost 19 20 I tried carrying the weight of the world 21 But I only have two hands 22 I hope I get the chance to travel the world 23 But I don't have any plans 24 I wish that I could stay forever this young 25 Not afraid to close my eyes 26 Life's a game? made for everyone 27 And love is the prize 28 29 So wake me up when it's all over 30 When I'm wiser and I'm older 31 All this time I was finding myself 32 And I didn't know? I was lost 33 34 So wake me up when it's all over 35 When I'm wiser and I'm older 36 All this time I was finding myself 37 And I didn't know? I was lost 38 39 I didn't know I was lost 40 I didn't know I was lost 41 I didn't know I was lost 42 I didn't know I was lost 43 44 So wake me up when it's all over 45 When I'm wiser and I'm older 46 All this time I was finding myself 47 And I didn't know? I was lost | 00 Feeling my way through the darkness 01 Guided by a beating heart 02 I can't tell where the journey will end 03 But I know where to start 04 05 They tell me I'm too young to understand 06 They say I'm caught up in a dream 07 Well life will pass me by if I don't open up my eyes 08 Well that's fine by me 09 10 Sing Chorus (the lyrics are below) 11 12 Sing Chorus (the lyrics are below) 13 14 I tried carrying the weight of the world 15 But I only have two hands 16 I hope I get the chance to travel the world 17 But I don't have any plans 18 19 I wish that I could stay forever this young 20 Not afraid to close my eyes 21 Life's a game? made for everyone 22 And love is the prize 23 24 Sing Chorus (the lyrics are below) 25 26 Sing Chorus (the lyrics are below) 27 28 I didn't know I was lost 29 I didn't know I was lost 30 I didn't know I was lost 31 I didn't know I was lost 32 33 Sing Chorus (the lyrics are below) 34 35 Chorus Lyrics: 36 So wake me up when it's all over 37 When I'm wiser and I'm older 38 All this time I was finding myself 39 And I didn't know? I was lost |
Discuss
- 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?
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
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 minutes)
Open a Project: Direct students to open Lesson9_App1 from the CSP-Widgets repository. This is the first of three apps students will investigate today.
App 1 - Explore Song Lyrics:This level is a continuation of the Explore activity in the warmup. Students should pay close attention to 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", or as they're called in Quorum, an "action". We're going to watch a short video that explains it in more detail.
Video: Watch the CS Principles: Defining and Calling Functions (Video) as a class. The video uses Javascript and Code.org's App Lab, but the core concepts are the same in Quorum. 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.
App 2 - 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 32 but called two times, on lines 21 and 27.
- 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 in the comments at the top of the file. 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.
App 3 - 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 (5 minutes)
Journal
Review the UpdateScreen pattern with students. Afterwards have them add to their journal definition for a function and a function call.
How does the UpdateScreen pattern work?
Many app projects run in the following way.
- The user interacts with a screen element (like clicking a button or typing in a text box). This triggers an event handler.
- The event handler changes the values stored in variables.
- The information on the screen is updated to reflect the change to the variable.
Typically every one of your event handlers (like the ButtonActivated
action) will include identical code for step 3, updating the screen. To avoid this problem, make a single function called UpdateScreen
that includes the code to change every element on your screen once variables have been updated.
You will usually want to call UpdateScreen
inside of every event handler and will also want to call it once at the beginning of your program. This ensures that the same code is always being used to update the information that shows up on the screen.
Closing
Discuss: Reflecting on today's lesson about functions:
- What did you learn?
- What are you uncertain about?
Discussion Goal
Discussion 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.
Assessment: Check for Understanding
For Students
Open a word doc or google doc and copy/paste the following question.
Question
In your own words describe the benefits of creating functions in your code.
Standards Alignment
- CSTA K-12 Computer Science Standards: 3A-AP-17, 3B-AP-23
- CSP2021: AAP-3.A.1, AAP-3.A.2, AAP-3.A.4
Next Tutorial
In the next tutorial, we will discuss CSP Unit 4 Lesson 10 Functions Practice, which describes Practice using functions.