//
Uncategorized

# Functions, Modules, and Types in Python

## Calling a function

Consider the following calls to the built-in type function for each of our fantastic-4 iterator types.

>>> type("42")
str

>>> type([1,2,3,4,5])
list

>>> type((1,2,3,4,5))
tuple

>>> type({‘a’: 1, ‘b’: 2})
dict

The name of the function is type, and it displays the type of a value or variable. The value or variable, which is called the argument of the function, has to be enclosed in parentheses. It is common to say that a function “takes” an argument and “returns” a result. The result is called the return value.

Instead of printing the return value, we could assign it to a variable:

>>> betty = type("42")
>>> print betty
str
>>> type(betty)
type

## Type conversion

Python provides a collection of built-in functions that convert values from one type to another. The int function takes any value and converts it to an integer, if possible, or complains otherwise:

>>> int("32")
32
>>> int("Hello")
ValueError: invalid literal for int(): Hello

int can also convert floating-point values to integers, but remember that it truncates the fractional part:

>>> int(3.99999)
3
>>> int(-2.3)
-2

The float function converts integers and strings to floating-point numbers:

>>> float(32)
32.0
>>> float("3.14159")
3.14159

Finally, the str function converts to type string:

>>> str(32)
'32'
>>> str(3.14149)
'3.14149'

It may seem odd that Python distinguishes the integer value 1 from the floating-point value 1.0. They may represent the same number, but they belong to different types. The reason is that they are represented differently inside the computer.

## Type coercion

Now that we can convert between types, we have another way to deal with integer division. Suppose we want to calculate the fraction of an hour that has elapsed using a variable called minute. The most obvious expression, minute / 60, does integer arithmetic, so the result is always 0, even at 59 minutes past the hour.

One solution is to convert minute to floating-point and do floating-point division:

>>> minute = 59
>>> float(minute) / 60
0.983333333333

Alternatively, we can take advantage of the rules for automatic type conversion, which is called type coercion. For the mathematical operators, if either operand is a float, the other is automatically converted to a float:

>>> minute = 59
>>> minute / 60.0
0.983333333333

By making the denominator a float, we force Python to do floating-point division.

## Math functions

In mathematics, you have probably seen functions like sin and log, and you have learned to evaluate expressions like sin(pi/2) and log(1/x). First, you evaluate the expression in parentheses (the argument). For example, pi/2 is approximately 1.571, and 1/x is 0.1 (if x happens to be 10.0).

Then, you evaluate the function itself, either by looking it up in a table or by performing various computations. The sin of 1.571 is 1, and the log of 0.1 is -1 (assuming that log indicates the logarithm base 10).

This process can be applied repeatedly to evaluate more complicated expressions like log(1/sin(pi/2)). First, you evaluate the argument of the innermost function, then evaluate the function, and so on.

Python has a math module that provides most of the familiar mathematical functions. A module is a file that contains a collection of related functions grouped together.

Before we can use the functions from a module, we have to import them:

>>> import math

To call one of the functions, we have to specify the name of the module and the name of the function, separated by a dot, also known as a period. This format is called dot notation.

>>> decibel = math.log10 (17.0)
>>> angle = 1.5
>>> height = math.sin(angle)

The first statement sets decibel to the logarithm of 17, base 10. There is also a function called log that takes logarithm base e.

The third statement finds the sine of the value of the variable angle. sin and the other trigonometric functions (cos, tan, etc.) take arguments in radians. To convert from degrees to radians, divide by 360 and multiply by 2*pi. For example, to find the sine of 45 degrees, first calculate the angle in radians and then take the sine:

>>> degrees = 45
>>> angle = degrees * 2 * math.pi / 360.0
>>> math.sin(angle)
0.707106781187

The constant pi is also part of the math module. If you know your geometry, you can check the previous result by comparing it to the square root of two divided by two:

>>> math.sqrt(2) / 2.0
0.707106781187

## Function Composition

Just as with mathematical functions, Python functions can be composed, meaning that you use one expression as part of another. For example, you can use any expression as an argument to a function:

>>> x = math.cos(angle + math.pi/2)

This statement takes the value of pi, divides it by 2, and adds the result to the value of angle. The sum is then passed as an argument to the cos function.

You can also take the result of one function and pass it as an argument to another:

>>> x = math.exp(math.log(10.0))

This statement finds the log base e of 10 and then raises e to that power. The result gets assigned to x.

## Adding new user defined functions

So far, we have only been using the functions that come  with Python, but it is also possible to add new functions. Creating new functions to solve your particular problems is one of the most useful things about a general-purpose programming language.

In the context of programming, a function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition. The functions we have been using so far have been defined for us, and these definitions have been hidden. This is a good thing, because it allows us to use the functions without worrying about the details of their definitions.

The syntax for a function definition is:

def NAME( LIST OF PARAMETERS ):
STATEMENTS

You can make up any names you want for the functions you create, except that you can’t use a name that is a Python keyword. The list of parameters specifies what information, if any, you have to provide in order to use the new function.

There can be any number of statements inside the function, but they have to be indented from the left margin. In the examples in this book, we will use an indentation of two spaces.

Here is a function with no parameters, so the syntax looks like this:

def newLines():
print
print

This function is named newLines. The empty parentheses indicate that it has no parameters. It contains two statements, and outputs 2 newline character. (That’s what happens when you use a print command without any arguments.)

• Creating a new function gives you an opportunity to name a group of statements. Functions can simplify a program by hiding a complex computation behind a single command and by using English words in place of arcane code.
• Creating a new function can make a program smaller by eliminating repetitive code. For example, a short way to print nine consecutive new lines is to call threeLines three times.

## Definitions and use

It is common in program design to declare several functions includeing a main() function, which indicates the start of the program and the first executable statements.

def main ():
# program execution will start here

def myfunction1():

def myfunction2():

#rest of the program

main()  # main function call

## Flow of execution

In order to ensure that a function is defined before its first use, you have to know the order in which statements are executed, which is called the flow of execution.

Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom.

Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called. Although it is not common, you can define one function inside another. In this case, the inner definition isn’t executed until the outer function is called.

Function calls are like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to the first line of the called function, executes all the statements there, and then comes back to pick up where it left off.

That sounds simple enough, until you remember that one function can call another. While in the middle of one function, the program might have to execute the statements in another function. But while executing that new function, the program might have to execute yet another function!

Fortunately, Python is adept at keeping track of where it is, so each time a function completes, the program picks up where it left off in the function that called it. When it gets to the end of the program, it terminates.

## Parameters and arguments

Some of the built-in functions you have used require arguments, the values that control how the function does its job. For example, if you want to find the sine of a number, you have to indicate what the number is. Thus, sin takes a numeric value as an argument.

Some functions take more than one argument. For example, pow takes two arguments, the base and the exponent. Inside the function, the values that are passed get assigned to variables called parameters.

The number and types associated with arguments are very important to the proper execution of any function. Since python types are determined at run-time it is important when programming that you consider what will happen when your function is called with argurments of different types.

Here is an example of a user-defined function that has a parameter:

```def dogyears(age):
if age < 0:
print "Negative age?!?  Back to the future."
elif age < 3 or age > 110:
print "Frankly, I don't believe you."
else:
print "That is ", age*7, "in dog years."
```

This function takes a single argument and assigns it to a parameter named age. This function should work for any numerical type argument (int or float). The value and type of the parameter is at this point unknown. When called the function will compare the value of the argument in a nested if statement to yield one of three alternatives.

>>> dogyears(7)
That’s 49 in dog years.

>>> dogyears(7.5)
That’s 52.5 in dog years.

We can also use a variable as an argument:

>>> mikesAge = 18
>>> dogyears(mikesage)
That's 126 in dog years.

Notice something very important here. The name of the variable we pass as an argument (mikesage) has nothing to do with the name of the parameter (age). It doesn’t matter what the value was called back home (in the caller)

## Variables and parameters are local

When you create a local variable inside a function, it only exists inside the function, and you cannot use it outside. For example:

def catsages(age1, age2):
cats = age1 + age2
dogyears(cats)

This function takes two arguments, adds them, and then calls our previous function.

When catsages terminates, the variable cats is destroyed. If we try to print it, we get an error:

>>> print cats
NameError: cats

Parameters are also local. For example, outside the function dogyears, there is no such named thing as age. If you try to use it, Python will complain.

## Stack and Frame diagrams

To keep track of which variables can be used where, it is sometimes useful to draw a stack diagram. Like state diagrams, stack diagrams show the value of each variable, but they also show the function to which each variable belongs.

Each function is represented by a frame. A frame is a box with the name of a function beside it and the parameters and variables of the function inside it. There is an interactive website that can help us visualize these frames.

The stack diagram for the previous example looks like this: The order of the stack shows the flow of execution. dogyears was called catsages, and catsages was called by main. When you create a variable outside of any function, it belongs to  a special frame called __main__, distinct from the main() function we declared.

Each parameter refers to the same value as its corresponding argument.

If an error occurs during a function call, Python prints the name of the function, and the name of the function that called it, and the name of the function that called that, all the way back to __main__.

For example, if we try to access cats from within dogyears, we get a NameError: and a  list of functions called a traceback. It tells you what program file the error occurred in, and what line, and what functions were executing at the time. It also shows the line of code that caused the error.

You can notice the similarity between the traceback and the stack diagram. It’s not a coincidence.

## Functions with results

You might have noticed by now that some of the functions we are using, such as the math functions, yield results. Other functions, like newLines and dogyears, perform an action but don’t return a value. We will use a return statement, usually appearing at the end of a function to return a value for the function.

## Glossary

function call
A statement that executes a function. It consists of the name of the function followed by a list of arguments enclosed in parentheses.
argument
A value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function.
return value
The result of a function. If a function call is used as an expression, the return value is the value of the expression.
type conversion
An explicit statement that takes a value of one type and computes a corresponding value of another type.
type coercion
A type conversion that happens automatically according to Python’s coercion rules.
module
A file that contains a collection of related functions and classes.
dot notation
The syntax for calling a function in another module, specifying the module name followed by a dot (period) and the function name.
function
A named sequence of statements that performs some useful operation. Functions may or may not take arguments and may or may not produce a result.
function definition
A statement that creates a new function, specifying its name, parameters, and the statements it executes.
flow of execution
The order in which statements are executed during a program run.
parameter
A name used inside a function to refer to the value passed as an argument.
local variable
A variable defined inside a function. A local variable can only be used inside its function.
stack diagram
A graphical representation of a stack of functions, their variables, and the values to which they refer.
frame
A box in a stack diagram that represents a function call. It contains the local variables and parameters of the function.
traceback
A list of the functions that are executing, printed when a runtime error occurs.