Lesson Summary

Summary

In Python, a function is a named sequence of statements that belong together. In this lesson, students learn why functions are used, how they are used, and how they are defined.  

Outcomes

  • Students learn how functions affect program flow, how functions use local variables, and how they use global variables.  
  • Students learn preferred means of communication with functions and how to incorporate them into their programs.  Attention is also given to debugging programs that contain functions.

Overview

  1. Getting Started (5 min)
  2. Guided Activities (35 min)
    1. Arguments and return values [5 min]
    2. Compound Functions [5 min]
    3. Function Definitions [5 min]
    4. Return Values [10 min]
    5. Line Numbers [10 min]
  3. Wrap Up (10 min)

Source

Students will work extensiviely with the online version of How to Think Like a Computer Scientist (HTLACS) hosted by Runestone Interactive. 

Learning Objectives

CSP Objectives

Big Idea - Creativity
  • EU 1.2 - Computing enables people to use creative development processes to create computational artifacts for creative expression or to solve a problem.
    • LO 1.2.3 - Create a new computational artifact by combining or modifying existing artifacts. [P2]
Big Idea - Abstraction
  • EU 2.2 - Multiple levels of abstraction are used to write programs or create other computational artifacts.
    • LO 2.2.1 - Develop an abstraction when writing a program or creating other computational artifacts. [P2]
Big Idea - Programming
  • EU 5.1 - Programs can be developed for creative expression, to satisfy personal curiosity, to create new knowledge, or to solve problems (to help people, organizations, or society).
    • LO 5.1.2 - Develop a correct program to solve problems. [P2]
  • EU 5.3 - Programming is facilitated by appropriate abstractions.
    • LO 5.3.1 - Use abstraction to manage complexity in programs. [P3]
  • EU 5.4 - Programs are developed, maintained, and used by people for different purposes.
    • LO 5.4.1 - Evaluate the correctness of a program. [P4]

Math Common Core Practice:

  • MP1: Make sense of problems and persevere in solving them.
  • MP5: Use appropriate tools strategically.

Common Core Math:

  • F-IF.1-3: Understand the concept of a function and use function notation
  • F-IF.4-6: Interpret functions that arise in applications in terms of the context
  • F-BF.3-5: Build new functions from existing functions

Common Core ELA:

  • RST 12.4 - Determine the meaning of symbols, key terms, and other domain-specific words and phrases
  • RST 12.10 - Read and comprehend science/technical texts

NGSS Practices:

  • 5. Using mathematics and computational thinking

Key Concepts

Students will understand the purpose of functions and how they allow a program to be built and maintained in a modular way.

Predictable Misunderstandings:

- students often think that functions (or any code) written will run.  They don't connect that it must be called in order to be run.

- students often think that functions have access to variables that they don't have access.


Essential Questions

  • How can computing and the use of computational tools foster creative expression?
  • How does abstraction help us in writing programs, creating computational artifacts and solving problems?
  • How are algorithms implemented and executed on computers and computational devices?
  • How are programs developed to help people, organizations or society solve problems?
  • How are programs used for creative expression, to satisfy personal curiosity or to create new knowledge?
  • How do computer programs implement algorithms?
  • How does abstraction make the development of computer programs possible?
  • How do people develop and test computer programs?
  • Which mathematical and logical concepts are fundamental to computer programming?
  • How does computing enable innovation?

Teacher Resources

Student computer usage for this lesson is: required

Lesson Plan

Getting Started (5 min)

  1. Show first 3:30 of Function Video: http://interactivepython.org/runestone/static/CCPS_Python/Functions/functions.html
  2. Students respond in their journals to the following prompts.
  • What is a function?
  • What is a parameter?
  • What are two types of parameters?

Guided Activities (35 min)

Arguments and return values [5 min]

  • Students read Python for Informatics: Exploring Informationd Chapter 4 Functions sections 4.1 to 4.3.  
  • Students copy the sentences below.  Replace the words argument and result with synonyms.
    • “It is common to say that a function "takes" an argument and "returns" a result. The result is called the return value.” 
  • Visit this web page (https://docs.python.org/2/library/functions.html) that lists the built in functions in Python.   
  • Verify that the Python Version is 2.7.
  • Ask, "How many built in functions are in Python 2.7?"
  • Change the Python version to 3.4.1.  How many built-in functions are in Python 3.4.1?
  • Why do you think there are a different number of functions in Python 3.4.1 than in 2.7?

Compound Functions [5 min]

  • Open Runestone Interactive Functions page (http://interactivepython.org/runestone/static/CCPS_Python/Functions/functions.html) and read about user defined functions in Python.
  • Read through lines 1 and 2 that define the header and body of compound statements.  Note the format of the compound statement used to define a function.
  • Ask:
    • What color is used to highlight the keyword def?
    • What punctuation mark ends the first( header) line?
    • How far is the body indented?
    • What color is the name of the function?
    • What word is used for the value in parenthesis?

Function Definitions [5 min]

  • Run ActiveCode 1.  
  • Change the number in line 15 and run the code to make a smaller square.
  • Change the number in line 15 and run the code to make a larger square.
  • Run ActiveCode 2.
  • Ask: What are the two line numbers used to call or invoke the function drawSquare?
  • Run ActiveCode 3.
  • Change the definition of drawMulticolorSquare to different colors.
  • Ask: What line would you change to draw smaller multi-color squares?

Return Values [10 min]

  • Some functions find and return values.  Complete ActiveCode 5, 6 and 7.
  • Change ActiveCode 7 so it prints the minimum values.

Line Numbers [10 min]

  • Run CodeLens 1.
  • Ask:
    • What line is executed next after line 6?
    • What line is executed next after line 3?
    • How do you think Python knew what line to go to after line 3?
    • What keyword is used to tell Python to return a value from the function named square?
    • What do you think would happen if you run the program after removing the return statement from line 3?

Wrap Up (10 min)

  • Return to Python for Informatics: Exploring Information Chapter 4 Functions.  Copy and paste the code from section 4.4 to Runestone ActiveCode 4.
  • Run the code multiple times.
  • Ask:
    • What do you note about the results?
    • What is the name of the function used?
    • Is the function on of the build in Python functions or is it defined in the program?
    • Where do you think the function is defined?
  • Add the number 1 as a parameter in the function call and run the program.
  • Explain the error message.
  • Write one thing to remember about functions on an exit note.

Homework:  Students should read Python for Informatics Chapter 4: Sections 4.7, 4.8 and 4.9.  Complete Exercise 2 and 3.  

Answer the following questions:

  •  What error message occured in Exercise 3?
  •  Why did the error occur?
  •  What happened when you change the order of print_lyrics and repeat_lyrics function definitions in Exercise 4?
  •  How is a function call like a road detour?
  • The parameter used to define the function print_twice is named bruce.  How would the function behavior change if the name was changed to wayne all three times it appears in the function definition?

Options for Differentiated Instruction

Students should be paired through this exercise since paired discussion is used for formative assessment.  Question can be provided to students through a variety of formats including production of a Google form or using student response systems.

Three sugested strategies are:

  • provide guide questions for reading
  • provide teacher provided annotated text excerpts
  • Students annotate text while reading

 

 


Evidence of Learning

Formative Assessment

After Activity D, Have students compare results with their elbow partners.  Discuss any unresolved issues.  This strategy can be used after any HTLACS check for understanding.

After Activity F, ask students to suggest a rule for creating functions that would help avoid this error.

Students work alternately between the web site, partners, and the whole group.  Teachers are to monitor student responses to the questions following each activity to be sure that students are addressing the key content within each activity.


Summative Assessment

  • Explain the advantages of functions/procedures in programming.
  • Explain the purpose of parameters with the use of functions.
  • Define a Python function with and without parameters.
  • Call a Python function with and without parameters.
  • Trace a function call from the main program to and from a user defined function.
  • Modify a Python program to use a function.
  • Create and use functions that return return a string or a number.