Functions

A "function" is a chunk of code with a name attached to it, so that we can re-use our code without having to type it all out over and over.

We've used functions that other people have written, such as "print()" and "random()". Now we are going to learn to write out own functions.

Question: how many ways are print() and random() different?

The keyword for creating functions is def, which is short for "define". All we do with def is tell it what we want to name our function, and then type out our code. As with "while" and "if", we do that by using a colon and indenting everything that is "inside" it.

Here we will create a function that makes a square, then call it three times:

width = 600
height = 400
win = GraphWin(width,height)
def drawSquare():
    p1 = Point( 10, 10 )
    p2 = Point( 60, 60 )
    r = Rectangle( p1, p2 )
    r.draw(win)
# now call it three times
drawSquare()
drawSquare()
drawSquare()

Or we could call the function inside a while loop:

while 1 == 1:
	drawSquare()
But that's not very interesting, is it?

Parameters

The first function we wrote does the same thing every time. What if we wanted to tell our function what to do? For example, instead of drawing the same square we want to tell it where to put it, and how big to make it?

To give your function parameters you just put the names you want to use inside the parentheses. Those names then become variables that you can use inside the function.

For example, we can write:

def drawSquare( pointA, pointB ):
    r = Rectangle( pointA, pointB )
    r.draw(win)

Now we can use that function by calling it with two Points as parameters:

drawSquare( Point(20,20), Point(60,60))

That probably still doesn't seem very interesting. All we've done is saved ourselves from having to type "r.draw(win)" each time.

But what if we want to actually do something more complicated, like draw a square using only the center point and a size? Look at this version:

For example, we can write:

def drawSquare( center, size ):
    p1 = Point( center.x - size / 2, center.y - size / 2 )
    p1 = Point( center.x + size / 2, center.y + size / 2 )
    r = Rectangle( p1, p2 )
    r.draw(win)

Note that the first parameter is a point, but the second parameter is just an integer. So to draw a square of size 100 centered at 150,100 we write:

drawSquare( Point(150,100), 100 )

Return Values

Ok, now we have a handy little function for making squares appear on the screen, but there's one thing I don't like about it: I don't like having the 'r.draw(win)' inside the function. It happens to work in this context, but there are two things I don't like about it:

What I would like to do is use it like our other shape-creation functions:

	win = GraphWin("Square",400,400)
	center = Point(100,100)
	size = 60
	s = makeSquare( center, size )
	s.draw( win )

If we try that with the last version of our function we get an error. (You could try it and see what error you get.)

What we need to do is give our function a Return Value. We used the term a couple times in class: the return value to random(), for instance, is a random decimal number.

Question: Can you figure out (experimentally) what the return value of print() is?

To make a function return something, all you have to do is use the word 'return' followed by the thing you want to return:

def makeSquare( center, size ):
    p1 = Point( center.x - size / 2, center.y - size / 2 )
    p1 = Point( center.x + size / 2, center.y + size / 2 )
    r = Rectangle( p1, p2 )
    return r   

And that's it! One tiny change...returning 'r' rather than drawing it (although I also renamed the function)...and we now have something that makes squares rather than just drawing them.

Oh, and make sure 'return' is the last thing your function does. You can't try to return something and then have the function keep going. Think of 'return' as meaning "return and exit".

Further Reading

A little confused and want to hear somebody else explain functions? You could try this tutorial or this one. For most new programmers, though, the hard part is not how you type it, but understand what return values and parameters are, and when/why you would want to use functions. So let's ask questions in Slack!