Functions

This tutorial was written for Python pre 3.0. A 3.0+ version of all my Python tutorials is planned.

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

Introduction to Functions


If it isn’t obvious, this tutorial isn’t a complete lesson in functions. It only serves as an introduction to functions. We will delve deeper into the subject in the future. All right, let us 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 girlfriend from scratch. You won’t get very far. And no, that blow-up doll doesn’t cut it. There are literally no limits to the types of functions you can create. So what is a function exactly? You can think of a function as a smaller program. For example take the code below.

1
print "Howdy!"

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

1
2
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 functions? Like this.

1
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.

1
2
3
4
def howdy():
    print "Howdy!"
 
howdy()

If howdy() came before the function definition you would get an error. 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 into our functions. The basic construct is below.

1
2
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 the code below does before cheating and reading ahead.

1
2
3
4
5
6
7
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 displays their sum. 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 whatever is calling the function. In this case, the two variables that were passed inside are added and the sum is returned.

The next few lines both get a number from a user. Since the value from raw_input() returns a string value (hey! raw_value() is also a function that returns something!), using a function to convert a string into an integer is needed. The function int() will allow us to do this. int() takes an input (in our case, what the user types) and gives us back (returns) an integer. We want to work with integers because we can’t really add strings.

Do note though, that trying to get the integer value of a string like “friend” will throw an error. And finally, the last line passes the two variables that hold the user’s input into the function, and prints the value of the function’s return.

Pretty simple, eh? There is one last thing in this section that I wanted 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:

1
2
3
4
5
6
7
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. I know that already! 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, 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. Like I said before, we’ll be covering them in greater detail later on. Since you’re so damn smart, you not only know what a function is now, 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. Isn’t Python cool!? I sure think it is! I hope you enjoyed this rather short tutorial, and if you have any questions or comments don’t hesitate to comment below. :)