Functions

Functions

You have been using functions in your programs already. int(), str() and raw_input() are each functions that we have used. These functions are special functions specific to Python but there really isn't anything special about them other than that they come with the language. We will learn how to create our own functions.

Functions (also known as methods or subroutines), like many things in programming come from math. If you can remember your Algebra class where you learned about the famous function f() as in f(x) = 2x + 3 you will likely be able to understand programming functions as well. In Algebra you may remember that functions accept inputs and produce outputs. The functions you are familiar with most likely created some kind of line or curve. The function that I referenced above will create a curved line (parabola). As we substitute various values for x, the function will output different results. Lets look at a table of some possible input values and the outputs:

x f(x)
-3 -3
-1 1
0 3
1 5
3 9

Notice that the function f() doesn't really care what anyone else calls the value going in to the function. It simply renames it to x once it gets the value. For example, lets say we had a variable called secret_number and we set the value to 5 and then pass secret_number to f(). The function f() doesn't care that we called that number secret_number because it immediately renamed the value to x at the beginning of the function. This is because of something we call scope (we will talk a lot more about scope in the next topic).

How can I make functions?

You can make your own functions by following a few rules:

  1. begin your function declaration with the def keyword
  2. give your function a name (names must conform to the same standards as variables)
  3. place parentheses next to the function name
  4. list the functions parameters (like we see in our f(x) function). If your function does not need arguments then you indicate this by having empty parentheses: def function_name():
  5. add a colon at the end of your signature (this indicates that we are beginning a block)
  6. indent the body of your function
  7. return any values that the caller might need using a return statement
  8. separate parameters to your function and any return values with commas

    def function_name(parameter1, parameter2):
     print 'do something in the function body'
     return 'any values', 'you need'
    

Its important to remember that this is just a function declaration. We haven't actually called the function yet. You have already been calling functions so you are familiar with how this works. If I were to call the function above I would simply invoke it by saying its name and passing in what it is asking for:

function_name('arg1', 'arg2').

Try creating a power function that accepts a number and a power to raise that number to and returns the answer!

Why should I make functions?

Functions are one of the central aspects to programming languages. They help us to separate code into its separate pieces instead of cobbling together massive scripts that are difficult to understand. So far our programs have been simple but you might be able to imagine that if we tried to write a complex program in that style that we have used so far that our file would be really big and not very organized. A function should do one thing which should be obvious from its name. A function called add() should simply add things. It should not also subtract things and then launch missiles. This is a poor way to design programs.

Functions also make it possible to avoid code duplication. If you ever find yourself copying code from one place to another, you should stop immediately and write a function that you could call from many places. This will make your code clearer and cleaner.

results matching ""

    No results matching ""