Week 9
Functions
Weekly Summary
In these first few weeks we have been focusing on code that gets input information from a human user and presents the results of calculations (processing) back to that same human user. This is a good way to start, but humans are often not the main audience/user for our code. While there is often a human at the end of the process, much of the calculation done is intended for the computer - either as the end user, or as an intermediary in a longer process that will only eventually end up at a human.
When we are writing code this is really intended to be used by the computer, rather than the human, we typically replace the input() and print() functions with other forms of getting input and providing output. The first way we do that is through the use of functions.
Recall from our prior Scratch Unit that we defined functions as:
This week we will learn how to write functions not only as code run by the computer, but also to provide encapsulation, modularity, and code appropriate for general re-use.
Learning Outcomes
By the end of the week students should be able to:
- Reading Code
- Given an existing function, and a set of input parameters to that function, identify the output returned by the function.
- Writing Code
- Given a problem statement that includes the name of a function and the parameters required by that function, write the Python code/definition of the provided function such that it solves the problem by returning correct output values.
- For now, functions will be limited to those using
- Mathematical Operators
- Boolean Operators
- Conditional Expressions
- For now, functions will be limited to those using
- Given a problem statement that includes the name of a function and the parameters required by that function, write the Python code/definition of the provided function such that it solves the problem by returning correct output values.
- Debugging Code
- Given a problem statement and a non-working Python function that attempts to solve the problem, identify where the function is incorrect and explain how to fix it.
Learning Materials
Textbook Readings
- Log on to your online textbook
- Read the following sections from your textbook
- 5.1. Function calls
- 5.2. Built-in functions
- 5.3. Type conversion functions
- 5.4. Math functions
- 5.5. Random numbers
- 5.6. Adding new functions
- 5.7. Definitions and uses
- 5.8. Flow of execution
- 5.9. Parameters and arguments
- Note. I think one of the questions in this section is really confusing. It gives the answers as:
- What it means by "on two separate lines" is
- 6
- 7
- or
- 6
- 9
- 5.10. Fruitful functions and void functions
- 5.11. Why functions?
- 5.12. Debugging
- This chapter goes for much longer. I do not expect you to read the materials that come after 5.16. These sections are beyond where I want you to be right now.
Our Lessons - Defining Functions
- VIDEO - Motivating Functions - QB Passer Rating
- VIDEO - Writing and running a function
- VIDEO - How much should I tip? (Functions that return a number)
- VIDEO - Odd or Even?, Version 1 (Functions that return a String)
- VIDEO - Odd or Even?, Version 2
- VIDEO - Contains a Vowel (Functions that return a Boolean Value)
- VIDEO - Calculate a paycheck (Functions that take more than one parameter)
- VIDEO - Can I join the Marines
Introducing CS50 Duck Debugger
- VIDEO: Introducing the AI based Duck Debugger
- Harvard has an AI enabled Rubber Duck Debugger that we will allow you to use in this class on everything EXCEPT the competency demos.
- This video explains how to use the tool and why we are ok with you using it.
Additional Explanations (Podcasts)
These videos add to the previous formal lessons. They are here as extra explanation on certain topics.
- What are modules and why does Python have them?
- VIDEO Explanation
- What other modules exist?
- VIDEO Explanation
- LINK Official Python Modules (approximately 200 depending on your specific distribution of Python)
- LINK pypi.org is a repository of MANY third-party projects (currently approaching 500,000)
- VIDEO Explanation
- LINK Math module documentation page
- What is in the Random module?
- VIDEO Explanation
- LINK Random module documentation page
- What does the textbook mean by "pseudo random"
- VIDEO Explanation
- LINK A good article from Slate
- LINK The Lava Lamp randomizer
- What is the difference between Arguments and Parameters (and, for that matter, variables)
- VIDEO: Explanation
- What is this thing called a "NoneType"?
- VIDEO: Explanation
Practice Materials
Parsons Problems
These problems present the complete code for a program, but in a mixed-up order. Your task is to arrange the pieces correctly to "write" the described program. These problems are a great first step in learning because they allow you to focus on the semantics of programming rather than the syntax.
- 5.15. Mixed-up Code Questions
- Problem 2
- Problem 3
- Problem 4
- Problem 5
"Solutions" for these four programs are included in the Code Walkthrough section below.
We will have you look at some additional ones from this section next week.
Debugging Problems
Fixing programs that are "almost" correct but not yet functional is a crucial skill. As teachers, we often assume students will develop this skill by debugging their own work. However, it can be beneficial to provide explicit practice in debugging. The following problem is designed to give you semi-guided practice in debugging.
The solution for this program is included in the Code Walkthrough section below.
(Paired) Practice Programs
Each week, I will provide you with several practice programs (non-graded) that I encourage you to attempt, with your partner, before tackling the graded assignments.
Code Walkthroughs
The following are videos and code solutions explaining the problems above, including the Parsons Problems, Debugging Problems, and (Paired) Programming Activity. You SHOULD NOT view these until you have either solved the activities yourself and want to see how I would do it, or you have tried at least twice and are still stumped.
Don’t jump to the "correct answer" too quickly. It’s beneficial to struggle with a problem for a day or two. We improve when we meet and overcome resistance. Learning comes from practice, so make sure to TRY before jumping to the solutions.
- Parsons Problem 1 - 5.15.2
- Parsons Problem 2 - 5.15.3
- Parsons Problem 3 - 5.15.4
- Parsons Problem 4 - 5.15.5
- Debugging Problem 1 - Corn in an Elevator
- Pair Practice 1 - howFar()
- Pair Practice 2 - discountPrice()
- Pair Practice 3 - zodiac()
- Pair Practice 4 - daaBins()
Graded Materials
The following programs are formally graded as part of the Unit 2 “Problem Set.” You should work on these individually. While working on these problems, you may refer to your notes, textbook, any programs you wrote, and my videos. However, you should significantly restrict your discussions with classmates. It is moderately acceptable to discuss how you solved a problem in general terms, but you should never show your code to a classmate, whether you are the one struggling or the one helping. You can talk about ideas, but not specific solutions.
Don't forget about the concept of Rubber Duck Debugging and the Rubber Duck AI from CS50. If you are getting stuck this week you should absolutely pull out your rubber duck and talk to it. If that isn't working, jump to Rubber Duck AI. Both are ok on your practice/homework (even the graded materials) although both will be off limits during your CD in two weeks.
- Graded Functions problem set, part 1 (part of the Unit 2 Problem Set).