Functions

Make your own blocks in Snakie with functions and inputs, and see how they become Python def statements

By Kevin McAleer,    4 Minutes

Page last updated November 20, 2026


Course Cover Image


You’ve drawn a square, a triangle and a hexagon. Each time you rebuilt nearly the same blocks. A function lets you build something once, give it a name, and use it as often as you like.


Make your own block

Open the Functions drawer.

The Functions drawer

Drag out the first block, to do something. Click the name and change it to polygon. Whatever you put in its mouth becomes the body of your function.

Now open Functions again. There’s a new block in the drawer called polygon. You just made a block.

Give it inputs

A function gets really useful when you can hand it values. Click the blue gear on your function block and drag in two inputs. Name them sides and size.

The inputs become variables you can use inside the function. Build this:

The polygon function as blocks

On the left are three calls to the function. On the right is the definition, the recipe they all share.

import turtle

def polygon(sides, size):
    for _ in range(sides):
        turtle.forward(size)
        turtle.right(360 / sides)

polygon(3, 100)
polygon(4, 100)
polygon(6, 100)

A triangle, square and hexagon drawn by one function

Reading the Python

  • def polygon(sides, size): defines the function. def is short for define. Nothing is drawn yet
  • The indented lines are the body
  • polygon(3, 100) calls it, with sides set to 3 and size set to 100
  • 360 / sides is the sum from the turtle lesson, now done by the program

Three calls, three shapes, one recipe. If you want every shape drawn with a thicker pen, you change one place.

Functions that give something back

Some functions work out an answer. The second block in the drawer has a return socket for that. Here’s one a robot might use:

def percent_to_duty(percent):
    return int(percent * 65535 / 100)

print(percent_to_duty(50))
print(percent_to_duty(100))

You met that sum in the last lesson. Wrapped in a function, it has a name that says what it’s for.

Why bother?

Without functions With functions
The same blocks copied again and again One definition, many calls
Fix a bug in every copy Fix it once
A long stack that’s hard to read Short stacks with names that explain themselves

Try it Yourself

  1. Add a call that draws an octagon.
  2. Add a third input, colour, and set the pen colour inside the function.
  3. Write a beep(times) function that plays a short tone that many times.
  4. Challenge: put a call to polygon inside a repeat block, turning 10 degrees after each one. What do you get?

Common Issues

Problem: Nothing happens when I run it. Solution: Add a call block. A definition on its own does nothing. Why: def only teaches Python the recipe. Calling it is what cooks the meal.

Problem: TypeError: function takes 2 positional arguments but 1 were given Solution: Fill in every socket on the call block. Why: The function needs a value for each of its inputs.

Problem: I renamed the function and the calls broke. Solution: Rename it on the definition block. The call blocks follow. Why: The calls are tied to the definition, so one rename updates them all.

Check your understanding

Quick quiz 1 of 3

What does def polygon(sides, size): do?

Quick quiz 2 of 3

In polygon(6, 100), what is sides?

Quick quiz 3 of 3

What does return do?

< Previous Next >

You can use the arrows  ← → on your keyboard to navigate between lessons.

Course complete

Congratulations, you finished Snakie Blocks - From Blocks to Python!

That's all 13 lessons done. Let people know, it's a great way to inspire someone else to start.

Share card: I completed Snakie Blocks - From Blocks to Python on KevsRobots
Post Bluesky LinkedIn Facebook


What are you looking for?
Watch Videos Get Ideas Learn Something Read a Review Read the Blog Search
... Z Z z