1 of 14

Lesson 5

Functions

2 of 14

  • Understand how a sequence of code works
  • Understand what a function is
  • Understand what a function with an argument is
  • Understand how to reuse code

By the end of this lesson, you will…

Learning Objectives

3 of 14

What is a function?

“Make me a cup of tea please”

How do you make a cup of tea?

What steps are involved?

How do we know this?

A function is a command which contains the steps needed to perform a task

Lesson 5

4 of 14

How does a function work?

We are going to create a function to draw 10 circles in a pattern

  • Go to Definitions and drag def block into the coding area
  • In the first blank, type circles this will define the name of the function

We have now created a function. But we need to add code to the function. This is the code that will be ran when the function is called.

Lesson 5

5 of 14

Using the function

We import turtle as per previous lessons.

The circles function will use a for loop. It will go round 10 times.

It will draw a circle with a radius of 20 pixels, then turn left by 36 degrees (360/10)

We then call the circles function and watch as it automatically draws the pattern.

Run the code, what happens? Does it work as expected?

Can you change it to do something else?

Lesson 5

6 of 14

A function with an argument?

We are going to create a function to draw any shape using Turtle

  • Go to Definitions and drag def block to the coding area
  • In the first blank, type sides and in the second type n
  • The name of the function is sides and it has an extra parameter of n. A parameter is a placeholder for data that will be passed into the function. The data that is passed into the function is called an argument.

The code inside the function is run when the function is called.

Lesson 5

7 of 14

Code inside the function

Inside of the function we add:

  • A for loop which uses n to set the range
  • We also use two turtle blocks to move forward and left
    • Can you remember what this calculation will do?

Lesson 5

8 of 14

Using a function

Inside a while True loop:

  • Drag function_name from Definitions
  • Type sides in the first blank
  • Into the second blank, type an integer for n.

Lesson 5

9 of 14

Using a function

What if we let the user decide on n:

  • Keep sides in the first blank
  • Into the second blank, drag int(1) from statements
  • Then drag input() from Statements and place on top of the int() block
  • For the input block, ask the user “How many sides does this shape have?”

Lesson 5

10 of 14

Importing the turtle blocks if you haven’t already.

Your code should look like this, click to test

Lesson 5

11 of 14

What happens?

By using the name sides, we call the function.

The user input captures the number of sides the shape has. This is saved to n which is then used to control the for loop.

It also forms part of the calculation to determine the turning angle.

Lesson 5

12 of 14

Why are functions useful?

Functions are powerful tools.

They are subroutines, small sequences of code inside the main code.

We can call the function, and come out of the main code, do the function, then come back to the code.

They enable us to reuse sections of code.

They keep our code tidy, and with fewer lines to write.

In our code we can draw any shape using one section of code.

Lesson 5

13 of 14

  • How to create a basic function
  • How to create a function with an argument
  • How functions can be reused for different outputs

What have we learnt?

Plenary

14 of 14

Next Lesson

Next Lesson we will use EduBlocks to build our own projects to create patterns using shapes and colours

Plenary