Cassie Nesler Guest Blog

Rubber Ducking: Function Fun using Python

This is Cassie, the coder from the novel Along the River Road.

I was hoping that since my guest blog last month, you might have spent some time messing around with Python and perhaps rtm. In this month’s guest blog entry, I’m going to up the fun quotient a ton to create functions using Python. Creating and using functions is one of the most important techniques you can learn in programming, no matter what language you are progging in.

 

Okay, snake handlers, let’s get our hands onto some Python progging!

 

If you did NOT sleep through algebra, you should remember what a function is. For example: f(x) = x2. This means that when you give the function some data, such as the number 2, it’s going to return 2 squared, or 4. Functions in Python work the same way. You give a Python function some data and it returns that data modified in some way. Python has two types of functions:

#1: built-in functions that come with the language, and

#2: user-defined functions.

 

We’ll look at the built-in functions first and then learn how to create our own functions for doing all sorts of useful stuff.

 

Doing the math in Python is fun because Python has all kinds of ultra-fly functions that do the calculations for you.

Need to find the square root of a number?

There’s a function for that.

Need to do some quick trig calculations?

There are functions for those also.

Functions are all nicely wrapped up in a library called the math library. To use the math library, you have to “import” it into the shell. Here’s how to import the math function:

 

>>> import math

>>>

 

Simple, right? Now, you’re ready to use the library to do some math.

 

Let’s start out with square roots. The function is named: sqrt

You use it like this to find the square root of a number, for example, take the numeral 9

 

>>> math.sqrt(9)

3.0

 

The number 9 is called the “argument” to the function and it is always placed inside parentheses (argument). Here’s another example:

 

>>> math.sqrt(12)

3.4641016151377544

 

As you can see, the sqrt function returns its answers with plenty of precision.

 

Let’s look at another example from the math library.

 

Say you want to compute what 3 to the 4th power. Python has a function for that – pow. The pow function takes two arguments, the base number and the power you want to raise it to.

 

For example:

 

>>> math.pow(3, 4)

81.0

 

Here’s another example:

 

>>> math.pow(12, 2)

144.0

 

Functions can be used in expressions.

For example, to compute the sum of the squares of two numbers, such as 4 and 5, we can write an expression like this:

 

>>> x = pow(4, 2) + pow(5, 2)

>>> x

41.0

 

There are many more functions like these in the math library, including the trigonometric functions.

 

Check out the online documentation at python.org for more information about the math library. Yes, rtm.

 

Pyhton makes it sooooo super simple-easy to work with built-in functions such as those found in the math library. I think, though, that it’s even more fun to create our own functions.

 

You do this in Python by creating a function definition.

 

For example, here’s a function definition for squaring a number:

 

>>> def square(x):

…      return x * x

>>>

 

Function definitions begin with the keyword: def. This lets the shell know that a function definition is coming. Next, you type in the function name, which in this case is square.

 

Following the function name is the “parameter”, or the data we are going to transform with the function. Function definitions can have one or more parameters and they can even have no parameters.

 

In this case we have one parameter, the number we want to square.

After the parameter list comes a colon, which tells Python that the function body is coming.

 

The function body is where you write the code that accomplishes the function’s task.

The body has to be indented from the first line of the function definition.

The second line of the function definition, the function body, contains a “return statement.”

The return statement is how you get the data from your function back to the program that is using the function. In short functions like this one, the return statement may be the only line of code in the function definition—That’s because in our example the computation to square a number is not too terribly complex – simply multiply the number by itself.

 

When the parameter is multiplied by itself, that value is passed from the function back to the place where the function was “called”.

 

For example:

 

>>> square(2)

4

 

The result is passed back to the shell and displayed as part of the REPL.

 

If the function is part of an expression, the value takes the place of the function call in the expression:

 

>>> y = square(2) + 3

>>> y

7

 

How about another example?

 

Say you want to convert temperatures from Celsius to Fahrenheit because you’re in Europe and when they say the temperature outside is 30 Celsius, you don’t know what that is in Fahrenheit.

 

Here is the function definition for that conversion:

 

>>> def ctof(temp):

…   return (9.0/5.0) * temp + 32.0

 

We can use the function in a program:

 

>>> celsius = 30

>>> fahrenheit = ctof(celsius)

>>> fahrenheit

86.0

 

If you want to be sure the function is correct, check it with 0 Celsius, which is 32 Fahrenheit, and 100 Celsius, which is 212 Fahrenheit.

 

You can write functions that work with strings as well as numbers. Also, functions can sometimes be more than one line long.

 

My friend the wordsmith, Sofia, wanted me to write a function that takes a word and makes it a plural by adding an “s” to the word and then spells out the new word. I made this function more than one line long by using a temporary variable to store the result of the string computation.

 

Here’s how it looks:

 

>>> def plural(word):

…   new_word = word + “s”

…   return new_word

 

Sofia tried it out and the results looked like this:

 

>>> plural(“dog”)

‘dogs’

>>> plural(“ant”)

‘ants’

 

Of course, this is an oversimplified version of how you make words plural.

 

Next time, I’ll expand this definition so that it can properly make a larger range of words into plurals. Next month in my next blog, I will look at how to make decisions in Python, so that we can look at words and decide whether they need “s” or “es” to make the word a plural.

 

In the meantime, rtm.

Leave a Reply