Functions

This is tutorial num�ro quatre in my Python Basics series. In this lesson we will become intimate with functions. Your significant other may even get jealous at of your new found love. And if you don’t have a significant other, your new significant other is the function!

Introduction to Functions

All right, let’s begin! If there is anything you’re going to take away from my extremely corny introduction, it’s that unlike your significant other, functions can be tailored to your liking. Try creating a girl friend from scratch. You won’t get very far. And no, that blow-up doll doesn’t cut it. Anyways, back to Python. There are literally no limits to the types of functions you can make. You can think of a function as a smaller program. For example take the program below:

print "Howdy!"

If we functionized (I made the word up) this program up, it would look something like this:

def howdy():
    print "Howdy!"

You don’t have to call it howdy, I’m just feeling very cowboyish, so I did. Do note that running this produces no output. All we have done is defined a function. So what’s the next step? Well, say we want to use this function. We would do this by calling it. How do we call it? Like this:

howdy()

That’s all there is to it. This wouldn’t work if the function wasn’t defined, though. And since Python is an interpreted language the code is run line by line from the top down. So, your entire code should look like this so far:

def howdy():
    print "Howdy!"

howdy()

Not a very imaginative program, but surely you can see how useful functions are. Now that you have the basic idea of what a function is, we can further develop your skill with them.

Passing Variables

Just like the header suggests, we’re going to be passing variables to our functions. The basic construct is:

def our_cool_function(variable1, variable2):
    DO SOMETHING WITH VARIABLES

It doesn’t get much simpler than that. And since I know for a fact that you understand the basic idea of functions, I’m going to give you an example. Try to tell what it does before cheating and reading ahead:

def addNums(num1, num2):
    return num1 + num2

userNum1 = int(raw_input("Give me a number: "))
userNum2 = int(raw_input("Give me one more: "))

print addNums(userNum1, userNum2)

Pretty wacky, huh? If the name of the function didn’t give it away, this program adds two numbers and prints them to the screen. We’ll go through it line by line, though. The first line defines our function, and gives it two “empty variables.” The second line has something you’ve never seen before. What on earth is a return? Well, unlike the first function you learned that just prints something, this function gives a value back to you instead. Anything after the “return” is given back to whoever is calling the function. In this case, the two variables that were passed inside are added and the sum of them is returned. The next few lines both get a number from a user. Since the values from raw_input() return a string value (hey! raw_value() is also a function that returns something!), using int() will convert it to an integer. Do note, though, that trying to get the integer value of a string like “google” will throw an error. Catching, and dealing with errors is beyond the scope of this tutorial, though. And finally, the last line passes the two variables that hold the user’s input into the function, and prints the value the function returns.

Pretty simple, eh? There is on last thing in this section that I want to mention. What if you’re only passing one value in, but the function expects two? Well, then you can default a parameter in the function. Take a look at the almost identical example below:

def addNums(num1, num2 = 0):
    return num1 + num2

userNum1 = int(raw_input("Give me a number: "))
userNum2 = int(raw_input("Give me one more: "))

print addNums(userNum1)

It’s a pretty stupid example. The key change is in the first line. You’ll notice that the second parameter is num2 = 0. What this says is if no second variable is passed to this function, num2 will default to the value of 0. If you pass a second variable, though, num2 will take on the value of whatever was passed like it normally would have. A pretty nifty feature, no?

Conclusion

There isn’t much else to cover when it comes to the basics of functions. Since you’re so damn smart, you not only know what a function is, but how to use one as well. You’re also starting to see that Python is full of internal functions like raw_input(), and anything else that has parenthesis. You’re also starting to see how beautiful this world is.

Leave a Reply

About

My ugly mug will go here along with a few sentences that will try to make me look cool.

Blog Categories