Unit 3, Chapter 1, Lesson 5: Creating Functions

Overview

In this lesson, students learn to define and call procedures (in JavaScript, procedures are called “functions”) in order to create and give a name to a group of commands for easy and repeated use in their code. They will be introduced to functions as a form of abstraction that enables them to write code in larger, more logical chunks and focus on what something does, rather than how it does it. As they explore the use of functions through a sequence of activities in App Lab, they will be asked to think about where they see the need for functions and how these functions can make their code clearer or more concise.

At the end of the lesson students review the concept of abstraction and are introduced to elements of the Create PT in preparation for the Practice PT at the end of the unit.

Purpose

Programming languages must necessarily define the meaning of a set of commands which are generally useful and applicable. In order to extend their functionality, nearly all programming languages provide a means for defining and calling new commands which programmers design to fit the needs of the problems they are trying to solve. Defining functions is an example of how computer scientists use abstraction to solve problems. A programmer will design a solution to a small, well-defined portion of the task and then give a name to the associated code. Whenever that problem arises again, the programmer can invoke the new function by name, without having to solve the problem again. This ability to refer to complex functionality by simple, meaningful names allows for programs to be written in more intuitive ways that reflect the relationships between different blocks of code.

Objectives

Students will be able to:

  • Recognize functions in programs as a form of abstraction.
  • Write a program that solves a turtle drawing problem using multiple levels of abstraction (i.e. functions that call other functions within your code).
  • Explain why and how functions can make code easier to read and maintain.
  • Define and call simple functions that solve turtle drawing tasks.

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.

New Code

Getting Started

  1. In the previous lesson we created simple turtle drawings using only four commands. At some point you probably wished that more commands were available to you. What was one command you wanted to be able to use and explain why you wanted to use it.
  2. Programming languages will always have some commands that are already defined, but there will be many instances when the exact command we want isn’t available. Today we’re going to start exploring a powerful feature of most programming languages that will allow us to overcome this issue and create commands of our own.

Activity–Programming with Functions

  1. Log in to code.org
  2. Work through the lesson on your own–be sure to READ EVERYTHING!!
  3. 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. At the end of this unit we will be doing a Practice Create PT that mimics most components of the AP Create PT. We’re going to quickly review those components now, but we’ll have opportunities to review and practice them in the next few lessons as well. For right now you don’t need to understand all the details, just the big picture.
  2. Open this file–read the following sections to give you an overview of your final project for this unit:
    1. Page 9–Performance Task: Create–Applications From Ideas has 3 major components (you will skip the first 2 at this time):
      1. Video
      2. Written Responses
      3. Program Code
    2. Pages 10-11–Skim the submission requirements and actually read all of prompts 2a to 2d–concentrate most on “abstraction” and prompt 2d
  3. Let’s start now making connections between writing functions and “developing abstractions” that helps us when writing to programs. Respond to the following in your Word Online file for this class:
    1. List the benefits of being able to define and call functions in a program. Who specifically gets to enjoy those benefits?
    2. How is the use of a function an example of abstraction?
    3. Which of the following 2 items are true about functions:
      1. Meaningful function names help people better understand programs.
      2. Meaningful function names help computers better understand programs.
      3. Functions in programming are useful mathematical tools for doing complex computations.
      4. Functions in programming are named groupings of programming instructions.
    4. In your own words explain at least one reason why programming languages have functions.

Leave a comment

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