Unit 3, Chapter 1, Lesson 6: Functions and Top-Down Design

Overview

This lesson presents a top-down problem-solving strategy for designing solutions to programming problems. Students use a worksheet to learn about top-down design, and then on paper, design a solution to a new turtle drawing challenge with a partner. Having practiced this approach on paper and in code, students will be re-presented with the 3×3 square challenge from an earlier lesson and asked to improve upon their old solution by designing multiple layers of functions.

Purpose

The main purpose here is to familiarize students with the concept of developing “procedural abstractions.” One of the main learning objectives in the CSP framework is: “2.2.1 Develop an abstraction when writing a program…”. Furthermore the AP Performance Task: Create requires students to “develop an abstraction to manage complexity of your program.” For all intents and purposes, developing abstractions starts with writing reusable functions (or procedures) in your code that encapsulate and give a name to multi-line segments of code that other parts of your code calls upon.

A technique for deciding what functions you should write is to look at the problem with a “top-down design” perspective. The process of creating software begins long before the first lines of code are written. Breaking a problem down into layers of sub-tasks, and writing well-named functions that solve those tasks is a creative act of abstraction. It also leads to good code that is more efficient, easier to read, and therefore easier to debug when problems arise.

In professional settings, teams of people first identify the problems and sub-problems the particular software will be addressing and how it will be used. This approach to designing software is critical when facing large-scale programming tasks. Once the problem is well understood, it can be broken into parts that teams or individual programmers can begin to work on solving at the same time. Full software systems take advantage of the power of abstraction; each programmer in a team can write code, assuming the subproblems will be solved and written by other teammates.

Objectives

Students will be able to:

  • Write a complete program with functions that solve sub-tasks of a larger programming task.
  • Explain how functions are an example of abstraction.
  • Use a “top-down” problem-solving approach to identify sub-tasks of a larger programming task.

Vocab

  • Abstraction – a simplified representation of something more complex. Abstractions allow you to hide details to help you manage complexity, focus on relevant concepts, and reason about problems at a higher level.
  • Function – A named group of programming instructions. Functions are reusable abstractions that reduce the complexity of writing and maintaining programs.
  • Top Down Design – a problem solving approach (also known as stepwise design) in which you break down a system to gain insight into the sub-systems that make it up.

New Code

Getting Started

  1. In the previous lesson we wrote a program that used layers of functions (functions that called other functions) to get the turtle to draw a diamond-shaped figure.
  2. Imagine that you have two programs that drew the diamond-shaped figure. One program uses functions as we did in the previous lesson. The other doesn’t use functions; it’s just a long sequence of the turtle’s primitive commands. Which program is more efficient? Make an argument for why one is more efficient than the other. Share your arguments for one program over the other … “Efficient” can mean several different things here:
    1. It could mean the total number of primitive operations performed by the turtle.
    2. It could mean number of lines of code.
    3. It could hinge on the ability to reuse code within your own code.
    4. It could be about the speed and clarity with which you can write the program.
      1. All of these are valid arguments for a student to make.
  3. Efficiency is an interesting thing to think about, but functions also introduce the ability to leverage the power of abstraction: when we write a function, it solves a small piece of a bigger problem. Having the small problem solved allows us to ignore its details and focus on bigger problems or tasks.
  4. Today we’ll get more practice with thinking about how to break problems down into useful functions.

Activity

  1. Get the Top-Down Design Worksheet & complete it:
    1. Students should work in pairs.
    2. Read the first page of the worksheet that describes the top-down problem solving process.
    3. Design a solution to the problem on the second page by writing down the functions they would write to solve the problem.
    4. After a pair has come up with a solution on paper, have them compare with another group to see similarities and differences.
      1. The point of having students compare top-down designs is mostly just to see that people think about problems differently. Pairs should move to Code Studio after working out a solution on paper.
      2. Evidence of good design
        1. An interesting thing to look at is the name of the top-level function students come up with, which should be a description of what they think they are drawing.
        2. Try to come up with descriptive function names that give insight to their thinking. A high-level function named something like drawThing is not that great. Something like snowflake or crosshairs or antenna is better because it lets the reader see what the programmer is thinking about.
  2. Log in to code.org
  3. Work through the lesson on your own–be sure to READ EVERYTHING!!
  4. Evaluate at least 1 program someone else did. Include the name of the person whose code you critiqued. Use the following criteria:
    1. The program draws the diamond
    2. The program defines four functions: right(); drawStep(); drawSide(); and drawDiamond(); The names are less important than the existence of four functions with this functionality.
    3. The program makes a single call to drawDiamond();
    4. The program looks clean and organized

Wrap-up

  1. When we layer functions – with functions that call other functions – we are creating layers of abstraction.
  2. In programming, writing functions helps us create layers of abstraction that allow us to design and create increasingly complex systems.
  3. We’ve seen layers of abstraction before in the design of Internet protocols, or in the binary encoding of information.
  4. Solving a fundamental piece of a problem that can be reliably reused in a different context frees us to think about more complex problems because we can trust the piece that has been solved and don’t have to worry about its details.
  5. Solving small problems – like how to send a single bit from one place to another – allows us to think about bigger problems, like sending numbers, or text, or images, to multiple people, over networks, in packets…etc., etc., etc.
  6. Where else in your life have you seen layers of abstraction? Connect the idea of layers of abstraction to some other activity. Include your response in your Word Online file for this class.
  7. There are many possible connections to make since almost any task in life can be broken down ad-infinitum. In the video about functions from the previous lesson, Chris Bosh makes a connection to basketball. Once you know how to dribble and shoot, then you learn some moves to do, and once you know that, you run plays that rely on the fact that those fundamental elements (or the problems) have been solved.
  8. In your Word Online file for this class, respond to the following:
    1. Which of the following statements about writing functions and Top-Down Design is NOT true?
      1. Writing functions helps manage complexity in a program.
      2. Top-Down Design leads to programs which feature multiple layers of abstraction.
      3. Two programmers solving the same problem using Top-Down Design should arrive at identical programs.
      4. Top-Down Design relies upon identifying subproblems of a larger problem.
      5. Top-Down Design assists in identifying the layers of functions that will be used to solve a programming problem.
    2. In the Create Performance Task you will be asked to identify an abstraction in your program and explain how it helps manage the complexity of the program. Functions are a form of abstraction. Pick a function you wrote in your solution to the 3×3 square problem and explain how it helps manage the complexity of your program.

Leave a comment

Your email address will not be published. Required fields are marked *