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:
-
You should save your resulting code in CodeSkulptor, and submit your CodeSkulptor URL on OWL-Space. These exercises are not graded, but we still want to see how students are doing.
-
For many of the exercises (as indicated), you should use OwlTest. This will test your code for errors. It will also test it for code style, although we haven't yet said much about code style.
OwlTest is being developed by Stephen Wong. If you find any bugs or find it hard to use, please give him feedback.
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.
-
What's the difference between
2+3
and"2+3"
? -
Complete the following code. It should print
COMP 200
.dept_code = "COMP" course_number = 200 course_code = ??? print course_code
-
As you've seen,
int()
gives you the integer part of a number, without rounding. How can you round a number to the nearest tenth? E.g., givenn = 13.88
, we want the result13.9
, while forn = 623.72
, we want623.7
. Make sure your formula also works for negative numbers. -
Given a string such as
my_string = "COMP 200 is my favorite class."
, how can you determine the number of lettera
's in the string?
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.
-
In the last class, you wrote some code to convert degrees Celsius to degrees Fahrenheit. Alas, you didn't know how to save your code then, nor write functions. Write a function that converts degrees Celsius to degrees Fahrenheit. The following provides an outline with test cases.
def celsius_to_fahrenheit(temp_celsius): ??? print "100 degrees C is ", celsius_to_fahrenheit(100), "degrees F." print "0 degrees C is ", celsius_to_fahrenheit(0), "degrees F." print "37 degrees C is ", celsius_to_fahrenheit(37), "degrees F." print "-40 degrees C is ", celsius_to_fahrenheit(-40), "degrees F."
Test your
celsius_to_fahrenheit
function using OwlTest. Be sure to save your CodeSkulptor code first and use the new URL for testing! -
Similarly, in the last class, you wrote some code to compute the surface area of a rectangular prism, given its width, height and depth. Write a function called
rect_prism_area
that takes a width, height and depth and does this computation. -
Write a function
make_sentence
that takes two strings and concatenates them into a sentence with the verb"is"
. For example,make_sentence("Rice","great")
results in"Rice is great."
. -
Calculating the distance between two points is useful in many situations. For example, it's useful in finding a good route on a map or for comparing two data points. Write a function
distance_from_origin
that takes two numbers,x
andy
, and compute's that two-dimensional point's distance to the origin. If you don't remember the relevant math formula, search for it online. For example,distance_from_origin(3, 4)
should return5
.
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:
-
x = 3 y = 7 z = 2 print x + pow(y, z)
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.
-
We examine the only complicated expression here:
-
x + math.pow(y, z)
-
-
Replace all the variables with their current values.
-
3 + math.pow(7, 2)
-
-
Compute
pow
's result.-
3 + 49
-
-
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.