# Activity 4 - Simplifying code with functions

We drew one hexagon. But this is just one of many hexagons Alex needs to build his honeycomb. Once again, it seems that we would have to repeat the code many times to draw multiple hexagons. Fortunately, we don’t have to! Functions to the rescue!

Here is an example of a function definition, and how we would use it:

def draw_line():
turtle.forward(50)
turtle.left(120)


The first line is called the function definition header. The def keyword tells the computer that we are defining a new function. Next, we give the function a name, in this case draw_line. Finally, don’t forget the (): at the end of the line, which tells the computer that we are starting the function body.

Similar to a for-loop, anything that we wish to define within the function needs to be preceded by 1 tab.

If you press run with just this code, you will notice that you won’t see any output! We’ve only created the functions, but we need to use them. To use the function we created, type in draw_line() again, but without the def.

def draw_line():    # Function definition
turtle.forward(50)
turtle.left(120)

draw_line()         # Function call


This is called a function call for the draw_line() function. A function call runs the code that’s defined in the function with the same name. With the recipe analogy, think of it as actually performing the recipe.

So, let’s try that out by making our own draw_hexagon() function! Remember to include the function definition header, the number of sides a hexagon has, and the angle associated with a hexagon: 60 degrees.