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
  • 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

 

Defining Functions

 

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 is the difference between Arguments and Parameters (and, for that matter, variables)
  • What is this thing called a "NoneType"?

 

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.

 

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.