Variables, Conditionals, and Functions - Lesson 10: Functions Practice

Overview

Goals

Students will be able to:

  • Write programs that use functions with the support of sample code
  • Debug programs that use functions
  • Identify opportunities to use functions to reduce repeated code within a program

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.

Resources

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.

Getting Started (5 minutes)

Remarks

  • Today we're going to have a chance to practice programming with a lot of the concepts and patterns we've explored over the last two lessons. I encourage you to go through these with a partner, but pay close attention to what each other is doing. In our next lesson you're going to have to use a lot of these on an independent project, and these activities are good practice for what you'll find there! Alright, let's get to it!
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 minutes)

Practice Time

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.

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

  • Today you're mostly going to practice what we've learned about programming with functions. I'm here to help you when you need, and you can put this cup on your computer when you need help. However, I first want to remind you of the following:
    • Use your debugging skills. Try to zoom in on precisely where you're getting stuck.
    • Talk to your partner! That's what they're there for!
    • Talk to the group next to you. If another group asks for help make sure to take some time to talk it through with them.

Apps 1 - 5 Declare and Call Functions: In these projects 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.

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.

Table with two columns of song lyrics labeled Style 1 and Style 2.
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

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.

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.

Wrap Up (5 minutes)

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

Discussion Goal

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

For Students

Open a word doc or google doc and copy/paste the following question.

Question

What will the value of the variable score be when the following code is finished running?

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

  • CSTA K-12 Computer Science Standards (2017): 3A-AP-17, 3B-AP-14

Next Tutorial

In the next tutorial, we will discuss CSP Unit 4 Lesson 11 Functions Make, which describes Make an app using functions.