Here you will learn
We have already used functions in this course, imported from the turtle module. For example penup() and left(angle). The former without an argument and the latter with an argument. Now we will learn how to create our own functions, both with and without arguments.
To define a function in programming means to give a block of code a name. The code in the function can be executed by calling the function, which is done by writing its name.
In Python, a function is defined with def, short for define. For example, we can write def function(): and then indent the code below that will belong to the function.
Functions can be used to avoid writing the same code over and over again. We start with an example and define a function square that draws a square. The function is then called by simply writing square().
Defines a function square that draws a square. Finally, the function is called.
def square():
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
square()
We can simplify the example above with a for-loop. Then we don't have to write the same thing four times.
The function square with the help of a for-loop.
def square():
for i in range(4):
forward(100)
left(90)
square()
Note the indentation in the example above. First, the line for i in range(4): is indented because it belongs to the function square. Then forward(100) and left(90) are in turn indented in further steps because they belong to the for-loop.
Login or create an account to save your progress and your code.
Read the code below and try to figure out what the program draws. Run the program after you answer and see if you were correct.
-- Output from your program will be here --
Question: What will the program draw?
With the help of arguments, we can send in values that are then used when the code in the function is executed. If we continue with squares as an example, we can use the side length of the square as an argument. We have already seen how functions with arguments are called, such as forward(100) where the argument is 100.
Defines and calls the function square with an argument.
def square(side):
for i in range(4):
forward(side)
left(90)
square(50) #draws a square with side 50
square(100) #draws a square with side 100
With our new arsenal of techniques, we can now draw arbitrary polygons. What we need to know is that the angles in a polygon with n sides are 360 / n.
Function to draw a polygon with n sides.
Click on run and try different values for n. What happens if n is large?
def polygon(n):
for i in range(n):
forward(500 / n) # divide by n for a suitable size
left(360 / n)
Login to save your progress and your code.
The function triangle takes an argument side which is the side of a triangle. The function's task is to draw an equilateral triangle with the side side. The rest of the code should remain.
-- Output from your program will be here --
Say we want to use the polygon function above and also be able to choose what color the polygon is or how long the sides should be. Then the function needs to receive multiple input values and thus have multiple arguments.
In the definition of the function, the arguments are written separated by a comma.
Multiple arguments. Function to draw a polygon with n sides and the color color.
def polygon(n, color):
color(color)
for i in range(n):
forward(500 / n) # divide by n for a suitable size
left(360 / n)
The polygon function above gives an error if we try to call it with only one argument. If we want the function to still work with one argument, we can give the argument color a default value. If the function is called without that argument, the default value is used.
The polygon function with the default value color = 'red'. Now the call polygon(3) works.
def polygon(n, color = 'red'):
color(color)
for i in range(n):
forward(500 / n) # divide by n for a suitable size
left(360 / n)
polygon(3) #draws a red triangle
Login to save your progress and your code.
Write a function square(side, color) where side is the side of the square and color is the color of the border.
-- Output from your program will be here --