When authoring programs with many visual elements, it can be helpful to separate elements of those drawings by returning the turtle to the origin.
We can do this using a goToHome function, using logic similar to what we just came up with in class a moment ago:
func goToHome() {
// Save current position
let currentX = turtle.currentPosition().x
let currentY = turtle.currentPosition().y
// Return to origin
turtle.penUp()
turtle.diagonal(dx: -1 * currentX, dy: -1 * currentY)
turtle.penDown()
}For example, when authoring this sketch in a prior year, Matthew Zhang used the goToHome function to isolate each part of his code:

TIP
This meant that if Matthew needed to change the turtle’s movements in an earlier part of his drawing, it would not impact the movements of the turtle in later parts of his drawing.
Matthew could have copy-pasted the code inside his function to do this:

However, that results in multiple lines of essentially identical code.
It is better to teach the computer how to return to the origin by defining a function:

And then invoking, or calling, the function whenever you want to make the computer carry out the behaviours described in the function:

Note how Matthew’s program invoked the goToHome function 31 times!
If he had simply copy-pasted the functionality, he would have used 186 lines of code:
By instead defining a function, and then invoking it multiple times, Matthew used this many lines of code:
Matthew’s program was significantly shorter as a result!
This is an example of applying abstraction – using functions.