COMP 200: Elements of Computer Science
Spring 2013

Practice with Functions

If you have not done so already, complete the first quiz on OWL-Space. In the future, you'll be expected to take the quizzes outside of class.

Like last class, work with a partner on the following exercises. Each work on your own computer (assuming enough people bring laptops), but collaborate with your partner.

There are two introductions for today:

Exercises — Finding and Using Built-in Operations and Functions

For the following problems, use Python's built-in operators and functions. For some, you'll need to find the appropriate built-in in the documentation.

Exercises — Defining and Using Your Own Functions

For the following problems, define a function to compute the desired result.

While your are working on these, do something crazy — intentionally make some mistakes. Why? When running broken code in CodeSkulptor, you'll learn some of its possible error messages. When using OwlTest, you'll see what kind of feedback that OwlTest provides.

More About Functions

The important thing to understand about functions is that calling, i.e., using, a function is equivalent to using the value that the function returns, i.e., computes. Note that we make a distinction between calling or using a function from defining a function, which is to write the code for one, something we will do in a moment. That is, conceptually, wherever we see a function being used in an expression, we can effectively replace that function call with the value that it returns.

Let's do an example to show this very important notion. Let us define 3 variables, then use them:

Is the result what you expect?

Now let's consider what Python is doing to get this result. In order to understand how to write a program, we need to understand the basics of what the computer will do with it.

  1. We examine the only complicated expression here:
    • x + math.pow(y, z)
              
  2. Replace all the variables with their current values.
    • 3 + math.pow(7, 2)
              
  3. Compute pow's result.
    • 3 + 49
              
  4. Compute the addition's result.
    •  52     
              

Note that we used the standard order of operations you're already used to.

So what does a function do for us? It encapsulates (packages) and abstracts (covers up and hides away) a potentially complicated calculation, enabling us to simply deal with the abstraction of that calculation. I.e., we only need to think about what will be calculated, not how.

This is a tremendously powerful notion. It means that we can take complicated calculations that may involves thousands of lines of complex code and wrap them up neatly in a function that we can pop into wherever we need. This keeps everything else relatively simple, enabling us to build even bigger, even more complex systems from building blocks of functions. This building technique will be the basis of how most everything in computing is done. The importance of understanding functions and how they work cannot be underestimated.