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
- 5.10. Fruitful functions and void functions
- 5.11. Why functions?
- 5.12. Debugging
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
Paired Programming Activity
Please complete the following Paired Programming activity with your assigned partner for the week.
Additional Explanations
Think of the following a little bit like podcasts. I will add videos here as I get questions from students or when I decide that I need to say something more beyond what is in the book. These won't "drop" on a particular day like the podcasts did in FCCS but will get added as the need arises. I would suggest you watch this space to see if new stuff has shown up. And if you have questions and feel there is a video that needs to go here PLEASE email me and let me know.
- 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 "pseudorandom"
- 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
Additional Practice
We will practice this more next week, so I won't assign any additional practice this week.
Code Walkthroughs
The following are code solutions for the programs discussed in the Paired Programming Activity. You SHOULD NOT view these until you have either solved the activities yourself - and you want to see how I would do it - or you have tried at least two different times and still are stumped. Don't jump to reading the "correct answer" too quickly. I think it is a GOOD thing to struggle with a problem for a day or two. We improve when we meet and overcome resistance. Learning comes from practice. You need to TRY before jumping to the solutions.
Again, this week only, I am not making videos because I think that seeing the code is sufficient. However, if you want me to explain something PLEASE ask.
- Func 1 - howFar()
- Func 2 - discountPrice()
- Func 3 - zodiac()
- Func 4 - daaBins()
Graded Practice
The following programs are formally graded as part of the Unit 2 "Problem Set." You should work on these only as an individual. While working on these problems you may refer to your notes, your textbook, any programs you wrote, and even my videos. However, you should limit your discussion of these programs with classmates. It is acceptable to discuss how you solved a problem in grand scheme of things, but you should never show your code to a classmate either as the person who is struggling or as the person who is helping a struggling classmate. You can talk ideas, but not specific solutions.
- Graded Functions problem set, part 1 (part of the Unit 2 Problem Set).