Today's lab focuses on functions from Chapter 6.
Since our first day of class, we have been using functions. This chapter revisits functions in more depth. In python, we indicate a function by following a name with parenthesis. For example, print() and main() are examples of functions. The print() is a built-in function in python, while the main() function is one that we write.
To define a function, we say:
Let's write a program that defines multiple functions. Start your program with the following:
# Demonstration program to graph function entered by user. # Prof. Melvin Fitting # October 24, 2011 from graphics import * from math import * def main(): print("This program will graph a function...") #Get user data #Set up window #Graph the function #Close the window main()We've set up an outline, in comments for our program. Let's fill in each part with a function. To make our main program easier to read, we will use functions, each of which does one part of the program. We will give our functions descriptive names so by reading through the main function we have an idea of what the program does.
We first need to get user data, so, let's make a function called getUserData(). It will take no input (also called parameters) but will need to return the data the user entered.
# Demonstration program to graph function entered by user. # Prof. Melvin Fitting # October 24, 2011 from graphics import * from math import * def main(): print("This program will graph a function...") #Get user data function, left, right, top, bottom = getUserData() #Set up window #Graph the function #Close the window def getUserData(): print("This program graphs an arbitrary math function.") print("You must enter the x and y ranges to be displayed.") left = eval(input("Enter x coordinate for left edge of window: ")) right = eval(input("Enter x coordinate for right edge of window: ")) bottom = eval(input("Enter y coordinate for lower edge of window: ")) top = eval(input("Enter y coordinate for upper edge of window: ")) function = input("Enter function expression to be graphed, with x as variable: ") return function, left, right, top, bottom main()What happens when you run this program? The main() function is called first. It prints the message, This program ... and then calls the getUserData() function. The getUserData() function is defined below the main() function. It prints a message and then prompts the user for the coordinates of the window and the function that should be graphed. Try running your program. Some inputs that you can use to test the program are: 0, 10, -100, 100, sin(x)*x**2. Nothing much will happen yet, since all we have done is asked for input.
Let's add more to the program. The next step is to set up the window. Add to the main() function:
win = setUpWindow(left, right, top, bottom, function)(this line should go right below the #Set up window comment).
After the getUserData, add in the function definition for the function that will set up a window:
def setUpWindow(left, right, top, bottom, function): win = GraphWin("Graph of " + function, 500, 500) win.setCoords(left, bottom, right, top) Line(Point(left, 0), Point(right, 0)).draw(win) Line(Point(0, bottom), Point(0, top)).draw(win) return win
Take a look at you program before you run it. When you type main(), it calls (or `invokes') the function main(). Inside main(), a message is printed, then there is a call to getUserData() function which returns 5 values that are assigned to the variables, function, left, right, top, bottom. The next line calls the function setUpWindow(). setUpWindow() takes 5 input parameters and creates a window object, win.
Looking at the main() function, there are still two last items to fill out in our outline of comments:
#Graph the function drawGraph(win, function, left, right) #Close the window win.getMouse() win.close()We need to add in a definition for the function drawGraph()
def drawGraph(win, function, left, right): increment = (right-left)/100 x = left rightPoint = Point(x, eval(function)) for n in range(100): leftPoint = rightPoint x += increment rightPoint = Point(x, eval(function)) Line(leftPoint, rightPoint).draw(win)Add this function definition after the one for setUpWindow(). We now have a complete program that makes multiple function calls. Try running it with the inputs: 0, 10, -100, 100, sin(x)*x**2.
Here's an overview of what happens when you run this program:
If you finish early, you may work on the programming problems.