Non-Programmer's Tutorial for Python 3/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 absolute values of 23 and 23 are equal.

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 absolute values of 23 and -23 are equal.

The key feature of this program is the  statement. (short for define) starts a function definition. is followed by the name of the function. Next comes a '(' followed by the parameter  (  is passed from the program into the function when the function is called).  The statements after the ':' are executed when the function is used.  The statements continue until either the indented statements end or a   is encountered.  The   statement returns a value back to the place where the function was called. We already have encountered a function in our very first program, the   function.  Now we can make new functions.

Notice how the values of  and   are not changed. Functions can be used to repeat tasks that don't return values. Here are some examples:

with output being:

Hello Hello Welcome Fred width = 4 height = 5  area = 20

That example shows some more stuff that you can do with functions. Notice that you can use no arguments or two or more. Notice also when a function doesn't need to send back a value, a return is optional.

Variables in functions
When eliminating repeated code, you often have variables in the repeated code. In Python, these are dealt with in a special way. So far all variables we have seen are global variables. Functions have a special type of variable called local variables. These variables only exist 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:

in print_func a = 17 a = 4

Variable assignments inside a function do not override global variables, they exist only inside the function. Even though  was assigned a new value inside the function, this newly assigned value was only relevant to , when the function finishes running, and the 's values is printed again, we see the originally assigned values.

Here is another more complex example.

output:

in a_func a_var = 15 in a_func b_var = 115 in a_func d_var = 30 in a_func e_var = 25 a_var = 10 b_var = 15 c_var = 125 d_var = Traceback (most recent call last): File "C:\def2.py", line 19, in    print("d_var = ", d_var) NameError: name 'd_var' is not defined

In this example the variables,  , and   are all local variables when they are inside the function. After the statement  is run, they all cease to exist. The variable  is automatically a local variable since it is a parameter name. The variables  and   are local variables since they appear on the left of an equals sign in the function in the statements   and.

Inside of the function  has no value assigned to it. When the function is called with, 15 is assigned to   since at that point in time   is 15, making the call to the function. This ends up setting  to 15 when it is inside of.

As you can see, once the function finishes running, the local variables and  that had hidden the global variables of the same name are gone. Then the statement  prints the value   rather than the value   since the local variable that hid the global variable is gone.

Another thing to notice is the  that happens at the end. This appears since the variable  no longer exists since   finished. All the local variables are deleted when the function exits. If you want to get something from a function, then you will have to use.

One last thing to notice is that the value of  remains unchanged inside   since it is not a parameter and it never appears on the left of an equals sign inside of the function. When a global variable is accessed inside a function it is the global variable from the outside.

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:

Options: 'p' print options 'c' convert from celsius 'f' convert from fahrenheit 'q' quit the program option: c Celsius temperature: 30 Fahrenheit: 86.0 option: f Fahrenheit temperature: 60 Celsius: 15.5555555556 option: q

area2.py

Sample Run:

Your Name: Josh Hello! Welcome, Josh To find the area of a rectangle, enter the width and height below. Width: -4 Must be a positive number Width: 4 Height: 3 Width = 4 Height = 3  so Area = 12

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. This program should include a menu interface.