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.