Lesson 19: Functions

Overview

Students learn how to create functions to organize their code, make it more readable, and remove repeated blocks of code. An unplugged warmup explores how directions at different levels of detail can be useful depending on context. Students learn that higher level or more abstract steps make it easier to understand and reason about steps. Afterwards students learn to create functions in Game Lab. They will use functions to remove long blocks of code from their draw loop and to replace repeated pieces of code with a single function. At the end of the lesson students use these skills to organize and add functionality to the final version of their side scroller game.

Purpose

In the first four lessons of this chapter students have learned to use a number of abstractions in their programs, including the velocity properties, isTouching, and collisions. These abstractions have allowed them to build much more complex programs while ignoring the details of how that behavior is created. In this lesson students learn to build abstractions of their own by creating functions.

Students will primarily use functions to break code into logical chunks that are easier to reason about. This foreshadows the chapter's transition from building technical skill to the organizational processes used to develop software.

Agenda

View on Code Studio

Objectives

Students will be able to:

  • Create and use functions for blocks of code that perform a single high-level task within a program
  • Create and use functions to remove repeated blocks of code from their programs
  • Create and use functions to improve the readability of their programs
  • Explain how abstractions allow programmers to reason about a program at a higher level

Vocabulary

  • Function - A named bit of programming instructions.

Introduced Code

Teaching Guide

Warm Up (10 mins)

High Level vs. Low Level Instructions

Prompt: Imagine you needed to write a 5-step set of instructions for going through your morning. What would they be? Write down your steps and be ready to share.

Discussion Goal

Goal: This conversations demonstrates to students that they often use a high level name or description for much more complex behavior. It is motivating the value of grouping or combining lots of smaller steps under a larger name and provides some of the justifications for creating functions within programs. Namely, high level steps make it easier to understand and reason about a process.

Content Corner

Breaking Down Processes: The order in which they are breaking down a larger task here also mirrors how they'll be asked to write code with functions. Often programmers first write the name of the function they intend to write based on what it should do before actually going in and writing the details.

Discuss: Student should write 5-step instructions for their morning and share with a neighbor. Ask a couple of students to share with the class.

Prompt: This set of instructions is pretty easy to follow and understand. They're at the level you might think about when describing your day to a friend. Now let's go a level deeper. Pick one of your 5 steps and split it into 5 smaller steps you need to complete that larger task. Be ready to share your ideas again.

Discuss: Students should share their smaller steps. Again ask some volunteers to subsequently share their original step and how they split it up.

Discuss: This is getting interesting. It seems like the first time we gave our steps we were "hiding" some of the details necessary to complete the task. Let's try this once more. Take one of your 5 smaller steps and split it up again into 5 even smaller steps.

Discuss: Ask students to share their steps one more time. Again ask some volunteers to share how they broke up one of their steps into even smaller steps.

Prompt: Imagine we had split every one of your first 5 steps into 5, and then split all of those steps again. This would mean we would have a high level set of instructions, and at the bottom a really low-level or detailed set of instructions. Be ready to respond to the following questions.

  • When would the high level set of steps you just wrote be most appropriate?
  • When would the lowest level set of steps be most appropriate?
  • Which one is easiest to reason about or understand?

Discuss: Ask students to share their thoughts and opinions. After a couple of minutes move the conversational to the transitional comment below.

Remarks

Sometimes details are important, but often high level steps are much easier to reason about and make it clear what's happening. In programs the same thing is true. We've learned that blocks like velocityX or isTouching actually just contain code that we could've written ourselves. Using these commands, or abstractions, is really helpful since we can think about code at a high level. Today we're going to learn how to group lots of commands to create a single new block our own. In programming when we create a new block like this we call it a function.

Activity (60 mins)

Transition: Move students into Code Studio where they will learn to create an call functions.

Code Studio levels

  • Collector Game
  • (click tabs to see student view)
  • Levels
  • (click tabs to see student view)

Wrap Up (10 mins)

Discussion Goal

Goal: Use this first prompt to review what students learned today. When they create a function they are creating their own block that they can call or use whenever they like. They saw at least two primary motivations for creating functions today including.

  • Simplifying code by breaking it into logically named chunks
  • Avoiding repeated code by making one block you can use multiple times.

Prompt: Why would we say that functions allow us to "create our own blocks?" Why is this something we'd want to do?

Discuss: Have students discuss at their table before talking as a class.

Discussion Goal

Goal: Students should review the definition of abstraction as "a simple way of representing something complex". Note that a function is an abstraction because it allows you to create one simple name for a more complex block of code.

Prompt: Write down your own definition of an abstraction? Why would a function count as an abstraction?

Discuss: Have students discuss at their table before talking as a class.

Remarks

Functions are a useful tool for helping us write and organize more complex pieces of code. As we start looking to end of the unit and your final project, being able to keep your code organized will be an important skills.

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 2-AP-11 - Create clearly named variables that represent different data types and perform operations on their values.
  • 2-AP-12 - Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
  • 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
  • 2-AP-14 - Create procedures with parameters to organize code and make it easier to reuse.
  • 2-AP-16 - Incorporate existing code, media, and libraries into original programs, and give attribution.
  • 2-AP-17 - Systematically test and refine programs using a range of test cases.
  • 2-AP-19 - Document programs in order to make them easier to follow, test, and debug.