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.
Students will be able to:
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.
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.
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.
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).
Journal: Review the UpdateScreen pattern with students. Afterwards have them add to their journal definition for a function and a function call.
Many app projects run in the following way.
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.
Prompt: Reflecting on today's lesson about functions:
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.
Question: In your own words describe the benefits of creating functions in your code.