Sep 14

Lecture Overview

Functions

Basics of python functions.

Here is example code defining a function:

def cube(x):
    print("Computing the cube of", x)
    r = x * x * x
    return r

The above code creates a function and stores it in a variable cube. The function stored into the variable cube has a single parameter (the variable x) and three statements. Note that when defining a function, the code in the indented block is not executed. The statements are just stored into the variable cube. We can then call a function as follows:

w = cube(2)
print(w)
print(cube(3))
print(cube(w - 2))

This will print

Computing the cube of 2
8
Computing the cube of 3
27
Computing the cube of 6
216

To call a function, we list the variable name for the function (in this case cube) followed by a parenthesized list of arguments, which are data values to match up to the parameters. What happens is that when python sees the expression cube(2), it first sets the parameter x to the value 2, records the place that the function should return to (in this case setting w), then starts executing the code stored in the cube variable. This prints a message, computes the cube, and then has a return statement. This causes the value in the variable r (in this case 8) to be returned, which means it is like return value 8 replaces the expression cube(2) and python continues execution by setting 8 into the variable w. Also after returning, the variables x and r are discarded.

Similarly, cube(3) is computed by setting x to 3 and executing the body of the function, which is the statements stored in the cube variable. The code computes 27 and returns it. The return value is then immediately passed to the print statement. The arguments to a function can be any expression, and the expression will be evaluated before calling the function. For example, the call cube(w -2) first computes w-2 to be 6 and then sets x to be 6 and executes the body of the function.

If no return value is given, the function automatically returns after the last statement, returning a special value None. For example, try running

def hello(name):
    print("Hello ", name, " it is nice to meet you.")

hello("John")
w = hello("Emily")
print(w)

Turtle

Turtle is a collection of python functions supplied by the python developers that let us draw on the screen. Here is an example.

import math
import turtle

# Draw a triangle

# The angle to draw a triangle
a = 2*math.pi/3
# The radius of the circle containing the triangle
r = 200

turtle.up()
turtle.goto(r,0)
turtle.down()
turtle.goto(r*math.cos(a),r*math.sin(a))
turtle.goto(r*math.cos(2*a),r*math.sin(2*a))
turtle.goto(r,0)

input('Press enter')

# A function to draw a red disk at a given location
def draw_disk(x, y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.begin_fill()
    # Set both the pen color and fill color to red
    turtle.color( (1.0,0.0,0.0), (1.0, 0.0, 0.0))
    turtle.circle(40)
    turtle.end_fill()

draw_disk(r, -40)
draw_disk(20, -12)

input('Press enter')

We are accessing two modules, math and turtle. A module is nothing more than a collection of functions. To access the functions within a module, we must first import it using the syntax import module_name. Now, any function inside the module can be accessed using syntax module_name.function_name. So for example, math.cos is the cosine function and turtle.up is the function that moves the pen up.

Exercises