Log in to save your progress.
<< Previous
Home
Next >>
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

Reference

Click on a function to read more.
Turtle
Function
Short description
back()
Moves the turtle backward
begin_fill()
Starts filling with color
circle()
Draws a circle with optional radius
color()
Determines the color the turtle draws with
end_fill()
Ends filling with color
forward()
Moves the turtle forward
goto()
Moves the turtle to a given coordinate
hideturtle()
Hides the turtle
left()
Turns the turtle left a number of degrees
pendown()
Puts the pen down on the drawing area.
penup()
Lifts the pen from the drawing area
right()
Turns the turtle right a number of degrees
shape()
Changes the drawing shape
Screen().setup()
Creates a drawing area with a given size
speed()
Sets the turtle's drawing speed

Functions

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.

Define and call functions

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().

Example

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.

Example

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.

What does the code do?

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.

Question: What will the program draw?

Ten squares in a grid pattern
Ten squares in a row
Ten squares with a common corner

Functions with arguments

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.

Example

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.

Example

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.

Modify the 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.

Functions with multiple arguments

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.

Example

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.

Example

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.

Modify the code

Write a function square(side, color) where side is the side of the square and color is the color of the border.

Status
You have finished all tasks in this section!
🎉