# Non-Programmer's Tutorial for Python 2.6/Defining Functions
### Creating Functions

To start off this chapter I am going to give you an example of what you could do but shouldn't (so don't type it in):
with the output being:
The program seems a little repetitive. Programmers hate to repeat things -- that's what computers are for, after all! (Note also that finding the absolute value changed the value of the variable, which is why it is printing out 23, and not -23 in the output.) Fortunately Python allows you to create functions to remove duplication. Here is the rewritten example:
with the output being:
The key feature of this program is the def statement. The def keyword (short for "define") starts a function definition. "def" is followed by the name of the function "absolute_value". Next, comes the single function parameter named, "n". A parameter holds a value passed into the function from the program that "calls" the function. Parameters of a function in the def statement, must be enclosed within a parenthesis. The value that is passed to a function parameter is called an argument. So for now, a parameter and argument points to the same thing. The block of indented statements after the ":" are then executed whenever the function is used. The statements within the function continue to be run until either the indented statements end, or a "return" statement is encountered. The return statement returns a value back to the place where the function was called in the calling program.
Notice how the values of a and b are not changed. Functions can be used to repeat tasks that don't return values. Here are some examples:
with output being:
That example shows some more stuff that you can do with functions. Notice that you can use one or more parameters, or none at all. Notice also that a function doesn't necessarily need to "return" a value, so a return statement is optional.

### Variables in functions

When eliminating repeated code, you often notice that variables are repeated in the code. In Python, these are dealt with in a special way. So far, all variables we have seen are global variables. Functions work with a special type of variables called local variables. These variables only exist within the function and only while the function is running. When a local variable has the same name as another variable (such as a global variable), the local variable hides the other. Sound confusing? Well, these next examples (which are a bit contrived) should help clear things up.
When run, we will receive an output of:
Variable assignments inside a function do not override global variables, they exist only inside the function. Even though a was assigned a new value inside the function, this newly assigned value exists only within the print_func function. After the function finishes running and the value of an a variable is printed again, we see the value assigned to the global a variable being printed.

### Complex example

The output is:
In this example the variables a_var, b_var, and d_var are all local variables when they are inside the function a_func. After the statement return b_var + 10 is run, they all cease to exist. The variable a_var is "automatically" a local variable since it is a parameter named by the function definition. The variables b_var and d_var are local variables since they appear on the left of an equals sign within the function in the statements: b_var = 100 + a_var and d_var = 2 * a_var.
Inside of the function a_var has no value assigned to it. When the function is called with c_var = a_func(b_var), 15 is assigned to a_var since at that point in time b_var is 15, making the call to the function a_func(15). This ends up setting the value of a_var to 15 when it is inside of a_func function.
As you can see, once the function finishes running, the local variables a_var and b_var that had hidden the global variables of the same name are gone. Then the statement print "a_var = ", a_var prints the value 10 rather than the value 15 since the local variable that hid the global variable is gone.
Another thing to notice is the NameError that happens at the end. This appears since the variable d_var no longer exists since a_func finished. All the local variables are deleted when the function exits. If you want to get something back from a function, then you will have to use return statement within the function.
One last thing to notice is that the value of c_var remains unchanged inside a_func since it is not a parameter and it never appears on the left of an equals sign inside of the function a_func. When a global variable is accessed inside a function, the function uses only value of the global variable but it cannot change the value assigned to the global variable outside the function.
Functions allow local variables that exist only inside the function and can hide other variables that are outside the function.

### Examples

temperature2.py
Sample Run:
area2.py
Sample Run:

### Exercises

Rewrite the area2.py program from the Examples above to have a separate function for the area of a square, the area of a rectangle, and the area of a circle (3.14 * radius ** 2). This program should include a menu interface.
Rewrite the area2.py program from the Examples above to have a separate function for the area of a square, the area of a rectangle, and the area of a circle (3.14 * radius ** 2). This program should include a menu interface.
| Previous: Debugging | Front Page | Next: Advanced Functions Example |