Move To
- Draw a star - function main() var size = 10 for 0 <= i < 5 do ♻turtle → forward(size) ♻turtle → left turn(144) end for end function
- What if I want the star in a particular location?
- We can pick up the pen, move to the location, put the pen down, and draw
- But it can be a bit of pain to calculate the turn angle and move distance to get to a particular location
- To make it easier, we can show a grid, turtle → show grid
- Note, that the coordinates may be different than you are used to
- The point (0,0) is in the upper left, and y values increase downward
- To move to a location on the grid can use - turtle → move to- function main() turtle → move to(200, 300) var size = 10 for 0 <= i < 5 do ♻turtle → forward(size) ♻turtle → left turn(144) end for end function
- The direction that the turtle is facing after a move to may be unpredictable
- If you are using the forward and turn functions, they assume a certain heading.
- To ensure a certain heading, use the - turtle → set headingfunction- function main() turtle → move to(200, 300) turtle → set heading(0) var size = 10 for 0 <= i < 5 do ♻turtle → forward(size) ♻turtle → left turn(144) end for end function
- If we want to draw a bunch of stars, we could use a loop, but then they would have to be in a regular pattern
- To put them at arbitrary locations without copying code, we need something new… 
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 star() for 0 <= i < 5 do ♻turtle → forward(10) ♻turtle → left turn(144) 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 star
- 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
- But now the draw circle makes the same size star every time
- We can modify the behavior of the function using paramters
- 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 size
- A parameter is a variable that gets it’s value set when the function is called. - function draw star(size: Number) for 0 <= i < 5 do ♻turtle → forward(size) ♻turtle → left turn(144) end for end function
- Use the new parameter in the main function
- Note, the main function has an error ‘not enough parameters supplied’
- Must specify the value, delete and re-add - function main() ▷ draw star(10) end function
- Now we can call the function multiple times and get multiple stars - function main() ▷ draw star(10) ♻turtle → pen up ♻turtle → move to(200, 300) ♻turtle → pen down ▷ draw star(20) end function
Multiple Parameters
- Can also have multiple parameters
- Add extra parameters for the x and y location to move to
- Use move to and set heading before drawing the star - function draw star(size: Number, x: Number, y: Number) ♻turtle → pen up ♻turtle → move to(200, 300) ♻turtle → set heading(0) ♻turtle → pen down for 0 <= i < 5 do ♻turtle → forward(size) ♻turtle → left turn(144) end for end function
- Now, it only takes one line to draw a star at different locations - function main() ▷ draw star(10, 200, 300) ▷ draw star(20, 300, 200) end function
- Notice that all of the parameters have : Numberafter their name
- This signifies their type, they are all numbers
- All of the variables we have seen so far have also been numbers
- One other type we can have is : Color
- To change the type of a parameter delete the current type and choose the desired type
- So to set the color of the star with a parameter - function draw star(size: Number, x: Number, y: Number, color: Color) ♻turtle → pen up ♻turtle → move to(200, 300) ♻turtle → set heading(0) ♻turtle → pen down ♻turtle → set pen color(color) for 0 <= i < 5 do ♻turtle → forward(size) ♻turtle → left turn(144) end for end function function main() ▷ draw star(10, 200, 300, colors → red) ▷ draw star(20, 300, 200, coloras → blue) end function
Quiz
- socrative.com
- Room name ‘INQ241’