Better Functions

Table of Contents

Grey overlay








Fork me on GitHub

1 Objectives

Developing the Programming and Development and Algorithm learning strands, specifically:

  • further developing understanding of repetitions (loops) in programming and being able to use for loops in creating patterns.
  • further developing understanding of functions in programming and being able to create functions that accept multiple parameters.

2 More on Functions


  • We previously created functions, which performed very specific tasks. Developer often try to make their functions as useful as possible by allowing them to take in different values, and give information back to the part of the program that called them. We'll have a look at this now.

Parameters 101

  • When we wrote our square function, the code looked like this…
def drawsquare():
    for x in range(4):
  • What if we wanted to be able to choose the size of our square?
  • Make a new Python program, and save it as ''.
  • Paste this code in:
import turtle
def drawsquare(sideLength):
    for x in range(4):

wn = turtle.Screen()
wn.title("Super Func-y")

tess = turtle.Turtle()
for myMoves in range(8):
  • You could use this code window instead, if you wish. Don't forget that any work you do here isn't saved, so you'll need to copy-and-paste out any work you want to keep, so you can save it elsewhere.
  • By adding sideLength to the first line, we're telling the function that when it runs, it will be provided with a number, which the function will be able to refer to as sideLength.
  • Did you notice that this time, in the line near the bottom of the code that calls the function, we've supplied a number to tell the function how big to draw the square: drawsquare(50)
  • In computer science, this is called passing in a parameter to the function. When you use print() in Python, you pass in text (a string, as we say). When you use tess.left(90), you're passing in a whole number (an integer)
  • This is handy, as we can now make squares of any size, and make ever nicer patterns. Change your 'for' loop at the bottom of the program from:
for myMoves in range(8):
  • to…
for myMoves in range(8):
  • Experiment with different ideas. You could…
    • Make a pattern where there the user chooses the number of steps (I used 8)
    • Write a function where the number of sides is a parameter
    • Create a function that takes a colour for the shape to be drawn
    • Anything else you can dream up!

More Parameters

  • Passing in one parameter is useful. It's even better if you pass in several. Copy and paste this code into a new, empty program and save it somewhere:
import turtle

def drawsquare(sideLength,newColour):
    for x in range(4):
wn = turtle.Screen()
wn.title("Func-y Town")

tess = turtle.Turtle()

growingSide=50 # The length of the sides we want to use.

for myMove in range(10):
    if myMove<5:
         drawsquare(growingSide,"red")  # Draw the square at its new size.
         drawsquare(growingSide,"blue")  # Let's mix it up.

    growingSide=growingSide+10 # Increase the value of growingSide by 10.

  • …or use this Python Trinket…

<iframe src="" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen=""></iframe>

  • You'll notice now that rather than using drawsquare(), we added the size we wanted inside the brackets instead, as well as the colour we'd like the square to be. This can give us the ability to create more intricate patterns, and eventually to start mixing up the colours.

3 Colouring in time

Badge it

  • Try coding some of these:
  • Silver: Modify the code so that it starts with a growingSide of 5, and doubles each time a new square is drawn.
  • Gold: If you've not done so before, write a function at the top of your code to draw a triangle, and another to draw a pentagon. Make them take at least one parameter to control the length of the sides. Look carefully at the functions for triangles, squares and pentagon. What's the same? What's different? Write a function called "polygon", into which you can pass a side length and number of sides, which then draws any polygon of any size for you. Tip: You might need to look over some of your earlier code to work out how to do the angles!
  • Platinum: Add an an ultimatePoly() function (you don't need to delete any other functions you've made. They won't run until they're called by you). It should take side length, number of sides, line colour and line thickness. Use it with a loop to make a complex pattern of your own.