Overview

In this lesson students spend most of their time practicing using the skills and processes they have learned about functions. At the conclusion of the lesson students discuss remaining questions in anticipation of their Make project in the following lesson.

Goals

Students will be able to:

Purpose

This lesson is students primary opportunity to get hands on with functions in code prior to the Make activity in the following lesson. Give students as much class time as you can to work through these. For this lesson it's recommended that you place students in pairs as a support and to encourage discussion about the challenges or concepts they're seeing. In the following lesson students are encouraged to work independently.

Getting Started (5 mins)

Remarks


Teaching Tip

Move Quickly to the Activity: There's a lot in the main activity of today's lesson. You may optionally wish to do a quick vocabulary review or address any questions that came up in the last lesson. Otherwise, give students more time to get hands on with some code.


Activity (35 mins)

Group: It is recommended that students work in pairs for this lesson and a number of the activities feature discussion prompts. Consider using pair programming, having drivers and navigators switch every 3 minutes, not every project.

Distribute: Optionally pass out a plastic cup or other manipulative they can place on their computer when they are stuck as a signal that they need support.

Open a Project: Today's lesson uses several apps. Direct students to begin by opening Lesson10_App1.

Remarks


Teaching Tip

Providing Support: Circulate around the room through the lesson encouraging students to use the strategies introduced at the beginning of the lesson. Students have a number of supports at their fingertips, so a big part of your role is helping build their independence in using those resources.


Apps 1 - 5 Declare and Call Functions: In these levels students practice declaring and calling functions. At first students practice calling functions that have already been declared for them. Students can focus their energy on the syntax of calling a function and how using functions changes the order in which lines of code run. Later in the progression they practice finding repeated code and are guided through how to create a function in its place.

Display: Watch the Scope Practice video below.

App 6 - Function Scope: This project returns to a topic that was covered in the variables lessons as well: variable scope. While students do not need a deep understanding of scope at this point, they will in some instances encounter debugging challenges that arise because of it.

Discuss: As a class, discuss the section below, "When to make a function".

When to Make a Function

An important questions for new programmers is "When should I decide to make a function". In general, the earlier you realize you need a function, the better. This is a skill you'll get better at with practice. By the end of this course you should aim to be in the During or Before rows.

When You Create FunctionsDescriptionComments
NeverYou never create functions at all.Your code will be difficult to read and debug and have lots of repeated code. Aim to move on at least to the "After" step.
AfterYou write your entire program without functions. Once you're done you look for repeated code and move those into a function.Your code is much easier to read and debug now. You're also getting better at seeing how your program is organized. As you get more comfortable, try to move on to "During" or "Before".
DuringAs you write your code you notice when you're about to rewrite code you already wrote somewhere else in your program. Before moving on you declare a function and call that function instead.You have a good understanding of your program and are able to keep it organized as you develop it. See if sometimes you can move on to "Before".
BeforeBefore you write your program you make a plan and identify places you're likely to use repeated code. You create your function at the beginning.This level reflects a strong understanding of how your program is going to be designed. You can almost "see it in your head" before you begin writing. That said, it's OK if sometimes you realize you need a function while you program and work in the "During" stage.

Teaching Tip

Never - After - During - Before: In this section students are introduced to a framework for thinking about their development with using functions. The primary question is "when should I make a function". In general, you want students to make their functions earlier, as this both improves the process of writing code and reflects deeper understanding of code structure. Throughout this course, you want to see them moving along the "never - after - during - before" scale.

At this point students are still learning to identify repeated code and replace it with a function. This would align with the "after" level. In the Functions Make project and the Unit 4 project they are encouraged to anticipate the need for functions in advance. Rather than write code twice and then remove the duplicate code by creating a function later, they should begin deciding early that they'll need a function. The "UpdateScreen() pattern" helps reinforce this point.

Reinforce this language in the classroom, though remember it's only a guide. Not every student will immediately be able to move on to "during" or "before". Different approaches also work better in different contexts, and many experienced programmers will typically operate in the "during" mode unless they're building a large and complex project.


App 7 - Creating Functions: In this project, students revisit the Movie Ticket app and are challenged to think through the process of declaring a function with code that they anticipate could be repeated.

Wrap Up (5 mins)

Prompt: What aspects of working with functions clicked today? What do you still feel like you have trouble with?

Discussion Goal: Use this opportunity to address any lingering questions or misconceptions in the room. You can also use this as a source of discussion topics to kick off the following lesson. As you lead the discussion, call out the many resources students have access to help when they're getting stuck.


Assessment: Check for Understanding

Question: What will be displayed after this code segment is run?

day_of_week = "Saturday"
day_of_week = "Monday"

IF ((day_of_week = "Saturday") OR (day_of_week = "Sunday")) {
     weekend
}
ELSE {
     weekday
}

PROCEDURE weekday {
     DISPLAY("School day")
}

PROCEDURE weekend {
     DISPLAY("Day off")
}

Standards Alignment