Computer Science 110
Foundations of Computing through Digital Media

Denison

CS 110 Lab Project 3

Due: By 5 p.m. Thursday, September 20th

In this lab, you will practice encapsulating sets of Python steps into functions. We will warm up with simple functions to repeat the calculation exercises from last week. Then we will use parameters with functions to repeat the two graphics assignments from last week. We will conclude with a re-coding of your Picture program.

For each of the programs, I expect good variable names and good commenting practices. There should be comments for the program itself, and we should comment each function so that the comments describe what the function does in terms of its parameters (sometimes called the function's 'givens') and details any return value and any output performed by the function.

Problem 1: Calculation functions

The following simple functions are characterized by having some parameter and then calculating a result based on that parameter. In these programs, we then output the result with a print statement, but in the future, we will learn how to have our programs return a value for use downstream in the program. Each of these problems will involve the definition of two functions: our main() function, with no parameters, and our calculation function. Note that we still want the operation of these programs to behave the same as before, so the main() function will take this into account.

Problem 2: Graphics Functions

Problem 3: Picture.py redesign

Using what you have learned about functions and parameters, redesign your Picture.py program to use functions. The top level sequence should be encapsulated by a main() function that creates the graphics window, executes the sequence of newly created functions, and ends by calling getMouse() and close().

Clearly, each of your Picture programs are different, and so you will each approach this assignment in different ways. If your program consists of a set of disparate elements, then your functions may perform simple grouping of statements. If your program repeats some sequence of steps, then your functions may, with parameters to distinguish a 'starting point' for the element, be an abstraction for drawing that element, and then be invoked repeatedly from your main() function.

Because variables defined in one function are not visible to other functions (think about the Function Call Stack picture), the variable for the graphics window you create in main() will almost always be an argument to your element functions (and a parameter in each of those function definitions), so that these element functions can draw to the same window created in main().