In this lab we will learn how to organize our programs into compact, reusable components, called functions. A function is a set of statements that have been collected together and given a name. It is essentially a sub-program. This ability to abstract a sequence of steps into a single name is one of the most important aspects of writing concise, clear programs.
In order to use a function we need to know what input to provide and what output to expect, in other words its external behavior. But we do not need to understand its internal operations. We can think of it as a black box. A function creates an interface for us to use. What are examples of things we use every day that provide an interface for us to interact with them?
To define a function you use the following syntax:
def <functionName>(<parameter1>, <parameter2>, ...): <statements>The inputs to a function are provided through the parameters. Also notice that the statements must be indented under the def.
To call a function you simply do:
<functionName>(<expression1>, <expression2>, ...)These expressions are called the arguments. The number of arguments in the function call must match the number of parameters in the function definition.
For example, suppose we wanted to create a function to compute area. We could define the function like this:
def computeArea(length, width): return length*widthThe return causes the function to output the resulting value.
Then we could call the function like this:
area = computeArea(12, 5) print("The area is", area)And it would print:
The area is 60When we call a function, each argument (in this case 12 and 5) is evaluated and assigned to the corresponding parameter (in this case length and width), and then the statements in the body of the function are executed in sequential order. For this simple function, there is only one statement.
Once we've defined a function, like area, we don't have to think about how it works. We can just reuse it again and again, passing in whatever values we need to compute. Here are a few more examples of calling this function.
x = 10 y = 5 result1 = computeArea(x, y) result2 = computeArea(2, 3)
We will use the graphics library to practice creating functions. To begin, open a terminal window on your desktop. Then type the following commands: to change into the s3p directory, to enter the special s3p environment, and finally to start up a new jupyter notebook.
cd s3p source /usr/swat/bin/s3penv juptyter notebookName this notebook DrawCircles.
Remember that in order to use graphics you'll need to first:
Let's create a function called drawCircle. It should take 5 parameters: a graphics window, an X coordinate, a Y coordinate, a radius, and a color. The function should create a circle at the given (X, Y) location and radius. It sets the fill of the circle to the given color, and finally it should draw the circle in the given graphics window.
Test your drawCircle function to be sure that it is working properly. Then use your function to create a bulls eye. Your bulls eye should consist of 4 circles that get increasingly smaller. The circles should alternate between 2 different colors as shown below. Feel free to pick whatever colors you'd like.
Hopefully you're starting to see why functions are so useful. Be sure to save and checkpoint this notebook before moving on.
Completing this notebook will be your homework. Once your program is working correctly, add markdown cells, interspersed between the code, to explain your program. The opening markdown cell should give a high-level summary. The markdown cell right above a function should explain what the function does and the purpose of each parameter to the function.
Open up a new jupyter notebook and name it DrawHouses. Do all of the necessary steps to create a graphics window.
Then, define a function called drawHouse. This function should take at least 5 parameters including, a graphics window, X and Y coordinates for the upper left corner of the house, the width of the house, and the height of the house. Your function should draw the house as a rectangle based on the given width and height with a triangle roof. See the example shown below. You may choose any colors you'd like for your house. You may also want to include extra parameters for the colors of the house and the roof, if you want to be able to easily change them.
Once you have the basic house structure in place, add some features to your house, such as a front door, a window, or a chimney. Be sure to define all of these features within your drawHouse function, and base their size and location on the function's parameters.
Once you're happy with your house, create a whole neighborhood of similar houses of different sizes (and possibly different colors) by calling your drawHouse function many times with slightly different arguments.