Review Circle
Draw a cricle using variables for circumference
function main() var circumference = 400 var number of sides = 36 for 0 <= i < number of sides do ♻turtle → forward(circumference / number of sides) ♻turtle → left turn(360 / number of sides) end for end function
- What if we wanted to draw many circles?
We could put it inside a loop
function main() var number of circles = 10 for 0 <= j < number of circles do var circumference = 400 var number of sides = 36 for 0 <= i < number of sides do ♻turtle → forward(circumference / number of sides) ♻turtle → left turn(360 / number of sides) end for end for end function
- But all the circles are in the same place and are the same size
Could move the turtle between circles
function main() var number of circles = 10 for 0 <= j < number of circles do var circumference = 400 var number of sides = 36 ♻turtle → pen up ♻turtle → left turn(360 / number of circles) ♻turtle → forward(circumference / number of circles) ♻turtle → pen down for 0 <= i < number of sides do ♻turtle → forward(circumference / number of sides) ♻turtle → left turn(360 / number of sides) end for end for end function
- That looks cool, but it’s getting to be a bit of a mess
We can do something to clean it up, functions
Functions
- A variables are named values, functions are named code
- Currently we have one function, main, it is what is executed when you choose run
- To make a new one, click on the script button above your code and choose ‘add new’
- From the menu, choose function
- Your code disappeared! Don’t worry we can get back to it
- What is created is a new function, it has the name ‘do stuff’ and does nothing, let’s fix that
- First change the name to draw circle, click on the name to change it to a text field
Now we can put whatever code we want inside this function, let’s put the circle drawing code here
function draw circle() for 0 <= i < 36 do ♻turtle → forward(400 / 36) ♻turtle → left turn(360 / 36) end for end function
- Now we can run, or call, this function from the main function
- To get back to the main function, click the script button at the top and click on the main function
Delete the code that draw the circle and replace it with the function call
▷ draw circle
- Using functions makes creating large programs easier
You have been doing this, every time you do turtle something, you are running code that someone else wrote
Parameters & Arguments
- But now the draw circle ignores the circumference variable
- So try to use circumference in the draw circle function
- TouchDevelop won’t let you
- This is because variables only exist in the function where the var command is used
- Note, we could use
var circumference
but this would create a new variable with the same name but a different value, not what we want - We need a way for the main function to communicate with the draw circle function
- Parameters and arguments, again we have seen this with turtle, then thing inside the parentheses
- If you click on the function name line, without clicking on the function name, because that changes the function name
- There is a + button for ‘add input parameter’
- click that and inside the parentheses after the function name there is a text box where you can name the parameter
- Name it cicum, just to differentiate it from circumference
- A parameter is a variable that gets it’s value set when the function is called.
- Use the new parameter
- Note, the main function has an error ‘not enough parameters supplied’
- Must specify the value, delete and re-add
- Can also have multiple parameters and of different types
Quiz
- m.socrative.com
- Room name ‘INQ241’