2 - 14
# Functions: Scope and Abstraction

**Revision Date:** Jun 27, 2015
(Version 1.2)

**Duration:** 2 50-minute sessions

0b10 - 0b1110

Unit 2. Developing Programming Tools

**Summary**

In the first portion of this lesson, students continue their inquiry into the properties of functions, with a focus on communication to and from other functions. In the lab portion of the lesson, students develop three Python modules using both Runestone Interactive and their Python IDE. Students use their own functions to perform calculations and draw a variety of polygons and a circle using turtle graphics.

**Outcomes**

- Students will understand the process of communication with functions.
- Students will be able to create and call functions.
- Students will be able to use both "fruitful functions" (sometimes just called
*functions*) and "unfruitful functions" (which do not return a value - these are usually called*procedures*). - Students will understand how functions simplify a project's creation.

**Overview**

**Session 1**

- Warmup (10 min)
- Exploring Functions (40 min)

**Session 2**

- Function Labs (45 min)
- Wrap-up (5 min)

- MP4: Model with mathematics.
- MP5: Use appropriate tools strategically.

- RST 12.2 - Determine central ideas and conclusions in the text
- RST 12.3 - Precisely follow a complex multistep procedure
- 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

- 1. Asking questions (for science) and defining problems (for engineering)
- 5. Using mathematics and computational thinking

- Students will understand the process of communication with functions.
- Students will be able to create and call functions.
- Students will be able to use both fruitful and unfruitful functions.
- Students will understand how functions simplify a project's creation.

- 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 do computer programs implement algorithms?
- How does abstraction make the development of computer programs possible?
- How do people develop and test computer programs?

Student computer usage for this lesson is: **required**

**In the Lesson Resources folder:**

- Slope Y-Intercept Project (to be developed)

**Other:**

- Python for Informatics: Exploring Information
- Runestone Interactive: How to Think Like a Computer Scientist
- Turtle graphics API documentation for Python 3.4.1: https://docs.python.org/3.4/library/turtle.html
- Circle Lab (http://interactivepython.org/runestone/static/thinkcspy/Labs/lab04_01.html)
- Triangle Lab (http://interactivepython.org/runestone/static/thinkcspy/Labs/lab04_01a.html)

- Display
*Python for Informatics: Exploring Information*Chapter 4 (Functions) - Section 4.11. - Students record in their journals four reasons to use functions and discuss with elbow partners to identify their two most important reasons.
- Ask students to identify two or three people they know who have the same name.
- Explain to their elbow partners who the people are and how they tell them apart when talking about them.
- Explain that we are going to working with functions today and that sometimes functions use the same names for different variables.
- Before writing programs with functions, we want to address the possibility of functions that use the same names.

**Teaching Note: **

Use of a format such as Google forms is sugggested for collecting student responses to the questions for each activity. It is important to keep this portion of the class moving so students have enough time for the labs. If students work with partners during their program development, they will have someone to share their progress with. Use a timer and have students briefly share their progress roughly every 10 minutes. This will not only help them understand an iterative development process, but also gives students practice with a collaborative development style.

- Display Section 4.12 of
*Python for Informatics*. - Explain: when progam developers write their own functions, they often run into problems (bugs) they need to fix. These debugging suggestions should help the students to prevent or correct problems.
- As students investigate and work with functions today, they should an eye out for function design problems and strategies used to prevent or solve them. We will add the rules we develop to this list.

- Students should open the Runestone Interactive Functions page (http://interactivepython.org/runestone/static/CCPS_Python/Functions/functions.html).
- Have students read "Variables and parameters are local" and run the Codelens to completion.
- Ask:
- What caused the error that occurred when the last line of the code was executed?
- What happens to local variables when a function finishes?
- Are parameter's values that are assigned inside a function also accessible outside the function?

- Run ActiveCode 9.
- What is the value of the variable
`power`

inside the function? - At what point was the variable
`power`

given that value? - This form of communication is risky, since programmers may not realize that a variable used in one code segment is also used inside a function elsewhere in the program.
- What is the recommended way to communicate values to a variable?
- Emphasize that avoiding global variables supports collaboration and reuse of code, since different authors can avoid interfering with each other's work.

- Complete the "Check for Understanding" after CodeLens 5, answering questions func-10, func-11, and func-12.
- Share "Check for Understanding" results either with elbow partners or the class as a whole.

- Go to the section entitled "The accumulator pattern."
- Do ActiveCode 10 and CodeLens 6.
- After reviewing the execution in the CodeLens, answer the "Check for Understanding" questions func-13 through func-14.
- See if the students can add any rules for creating and using functions to the list from
*Python for Informatics*.

- Go to the section entitled "Functions can call other functions".
- Do CodeLens 7 and ActiveCode 12.
- Ask:
- What function is started first when the code in ActiveCode 12 is executed?
- What function is finished first when the code is executed?

- Preview the three lab projects that students will complete and help students to get started with the Slope Y-Intercept project.
- Discuss the iterative development process after watching the video "Design as an Iterative Process"
- http://www.youtube.com/watch?v=9NxWW4poljU

- Complete the Slope Y-Intercept Project and the Drawing a Circle Labs.
- The Slope Y-Intercept Project is described in the document of the same name.
- The document includes descriptions of two functions to complete and contains a small sample of test data.
- The Slope Y-Intercept Project is in the Lesson Resources folder along with the starter Python file.

- Continue in Runestone to the Lab entitled Drawing a Circle (http://interactivepython.org/runestone/static/thinkcspy/Labs/lab04_01.html).
- In this second lab, students develop a function that uses a turtle to draw a circle.
- After finishing the lab, students should copy their code to a Python module in PyCharm.
- Students should create three functions named
`drawSquare`

,`drawTriangle`

, and`drawOctagon`

, along with main module code that calls each function.

- After completing the Drawing a Circle lab, students should follow the link below to the Lessons from a Triangle lab.
- Students should create a second Python module with the code from the starting point for the
`drawPolygon`

function code and finish implementing the`drawPolygon`

function. - Students should complete the Function Labs by developing the
`drawCircle`

function in Finally a Circle (the second half of the Lessons from a Triangle lab).- Additional components of the lab can be used as extensions for more able students.

- After submitting the labs, students present their labs with elbow partners and groups.
- Students reflect on the following prompts about writing programs with functions.
- What problems did they encounter?
- How did they overcome the problems?
- How do functions simplify the task of developing a program?

Iterative development works by developing, then sharing, programs at many points during the development process. Students should work in pairs as they create their programs and share the work through various completion stages.

Students who are completing projects quickly should be introduced to the Python turtle API at (https://docs.python.org/3.4/library/turtle.html). Students can investigate and implement such methods as `fill`

, `speed`

and others as described by the API documentation.

Check for understanding by assessing student performance on the Runestone Interactive questions. Students should first try to resolve any difficulties with their partners and groups.

Students should be able to make suggestions for creating and using functions.

Identify and address any areas discovered that students have been unable to come to a consensus understanding.

Create functions that receive parameters, perform calculations using those parameters, and return a value.

Write a function to return the slope and y-intercept of a function of the line through two points.

Write functions to create a variety of polygons and a circle.