Funky Functions

Table of Contents

Grey overlay

Pink

Green

Blue

Cream

Liliac

Purple

Yellow

Fork me on GitHub

1 Objectives

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

  • developing understanding of repetitions (loops) in programming and being able to use for loops in creating patterns.
  • developing understanding of functions in programming and being able to create simple functions that serve a single purpose.

2 Func-y Town

When developer write code, sometimes their programs need to repeat the same chunks of code over and over again. Programming languages help developers with this by allowing the use of Functions.

  • The definition we use is, "A Function is a piece of code which performs a task, and can return a value".
  • Let's look at an example.

Learn it

  • I'd like to draw this pattern…

pattern1.png

  • In order to do this, I need to start by drawing a square…

pattern1a.png

  • Then rotating the turtle 45 degrees

pattern1b.png

  • draw another square…

pattern1c.png

  • Turn another 45 degrees

pattern1d.png

  • and again…

pattern1e.png

  • And so on. If you think about it, what I'm essentially doing is saying
Repeat the following 8 times:
    DRAW A SQUARE
    ROTATE THE TURTLE 45 degrees
  • We can teach the computer how to draw squares (or anything else for that matter) by writing a function which contains the steps needed to perform that task.
  • We're going to start by creating a function called drawsquare().

Code It

  • Start a new Python program, and save it straight away. Call it pattern.py.
  • Start with the usual code to set Python up for drawing graphics…
import turtle

def drawsquare(): # This is a function called drawsquare
    for myMoves in range(4):
        tess.forward(100)
        tess.left(360/4)
        
wn = turtle.Screen()
wn.bgcolor("white") 
wn.title("Func-y Town")

tess = turtle.Turtle()
tess.color("hotpink")
        
for myMoves in range(8):
    drawsquare()
    tess.left(45)
        
wn.mainloop()
  • Use the code trinket below to experiment with the code a little, but be aware that you'll need to copy and paste your code somewhere else if you want to save it.

<iframe src="https://trinket.io/embed/python/5ef1bc1b9e" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen=""></iframe>

  • When you run this, it'll draw the pattern for you.
  • The def drawsquare(): line and the 3 lines that come after it are where we have declared (created) our function. We've said to Python that any time in the future when we write drawsquare(), we want a square drawing.
  • Change the 8 in the line: for myMoves in range(8) to something else.
  • Change the 45 in tess.left(45) to something else.
  • Can you manipulate the code to draw this shape?

pattern2.png

Badge It

  • Silver: Create a second function, which draws a triangle when it is called (used).
  • Add one line of code to your last program to produce this shape:

pattern2a.png

  • Gold: Write a program to make the shape below. It is made from 20 squares.
    • What angle will you need in between squares? Here's a hint to get you started:
    • A pattern with 4 squares would need 90 degrees between each one.
    • A pattern with 8 squares would need 45 degrees between each.
    • A pattern with 180 squares would need 2 degrees between each.
    • Is there a relationship between the two numbers?

pattern3.png

  • Platinum: You should now have a function for drawing a square, and one for a triangle. Write another function called squiangle() that when called (used) draws a square, then a triangle by calling those functions.

Back to homepage