Better Functions

Table of Contents

Grey overlay

Pink

Green

Blue

Cream

Liliac

Purple

Yellow

Fork me on GitHub

1 More on Functions

Recap

  • 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):
        tess.forward(100)
        tess.left(90)
  • What if we wanted to be able to choose the size of our square?
  • Make a new Python program, and save it as 'functions2.py'.
  • Paste this code in:
import turtle
        
def drawsquare(sideLength):
    for x in range(4):
        tess.forward(sideLength)
        tess.left(90)

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

tess = turtle.Turtle()
tess.color("hotpink")
        
for myMoves in range(8):
    drawsquare(50)
    tess.left(45)
        
wn.mainloop()
  • 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):
    drawsquare(50)
    tess.left(45)
  • to…
squareSize=50
for myMoves in range(8):
    drawsquare(squareSize)
    squareSize+=10
    tess.left(45)
  • 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):
    tess.color(newColour)
    for x in range(4):
        tess.forward(sideLength)
        tess.left(90)
        
wn = turtle.Screen()
wn.bgcolor("white") 
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.
    else: 
         drawsquare(growingSide,"blue")  # Let's mix it up.
    tess.left(36)

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

wn.mainloop()
  • …or use this Python Trinket…
  • 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.

2 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.