Swift Playground Part 2: Functions

In this section we learn about functions. We talk about what they are, how to create them and even how to call a function from a function. Let’s take a look.

Composing a new behaviour

You will have noticed that there isn’t a turn right command. This is because we are going to make a right turn ourselves which will be made up or “composed” of three left turns. Try it out.

Composing a new behaviour - Solution
moveForward()
moveForward()
moveForward()

turnLeft()
turnLeft()
turnLeft()

moveForward()
moveForward()
moveForward()

collectGem()

Creating a new function

In this challenge we will take this new turn left command and transform it into a function.

Creating a new function - Solution
func turnRight(){
    turnLeft()
    turnLeft()
    turnLeft()
}

moveForward()
turnLeft()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnLeft()
moveForward()
toggleSwitch()

Collect, toggle, repeat

Here the challenge is to define a function, but the difference is that it will be for a repeating pattern. Can you see where the pattern repeats itself? If not, check out the answers below.

Collect, toggle, repeat - Solution
func collectGeAndToggleSwitch(){
    moveForward()
    collectGem()
    moveForward()
    toggleSwitch()
    moveForward()
}

collectGeAndToggleSwitch()
turnLeft()
collectGeAndToggleSwitch()
moveForward()
turnLeft()
collectGeAndToggleSwitch()
turnLeft()
collectGeAndToggleSwitch()

Across the board

This challenge requires you to identify the pattern and then repeat it several times. Start with collecting three gems and transform this into a function.

Across the board - Solution
func collectThreeGems(){
    collectGem()
    moveForward()
    collectGem()
    moveForward()
    collectGem()
}

collectThreeGems()

turnRight()
moveForward()
turnRight()

collectThreeGems()

turnLeft()
moveForward()
turnLeft()

collectThreeGems()

Nesting patterns

Now that you are getting the hang of functions, we introduce how we can call a function from inside another function. Again, solve the challenge step by step and take a look at the answers if you get stuck.

Nesting patterns - Solution
func turnAround(){
	turnLeft()
	turnLeft()
}

func solveStair(){
    moveForward()
    collectGem()
    turnAround()
    moveForward()
}

solveStair()
solveStair()
turnLeft()
solveStair()
solveStair()

Slotted stairways

In this challenge we continue learning the concept of calling a function within a function thinking about how we can create functions based on a purpose such as solveRow() or collectGemTurnAround(). Have a go and check out the solutions below. Can you tell the difference between the two?

Slotted stairways - Solution 1
func collectGemTurnAround() {
	moveForward()
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    moveForward()
}

func solveRow() {
	collectGemTurnAround()
}

solveRow()
solveRow()

turnRight()
moveForward()
turnLeft()

solveRow()
solveRow()

turnRight()
moveForward()
turnLeft()

solveRow()
solveRow()
Slotted stairways - Solution 2
func collectGemTurnAround() {
	moveForward()
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    moveForward()
}

func solveRow() {
	collectGemTurnAround()
	collectGemTurnAround()
}

solveRow()

turnRight()
moveForward()
turnLeft()

solveRow()

turnRight()
moveForward()
turnLeft()

solveRow()

Treasure Hunt

This final challenge can be solved in many different ways. The way to attack this is to find the patterns and then solve each pattern step by step creating functions that have a specific purpose.

Using the sample solution below, try and improve it.

Treasure hunt - Solution 1
func moveThenToggle() {
    moveForward()
    moveForward()
    toggleSwitch()
}

func turnAround() {
    turnLeft()
    turnLeft()
}

func moveForwardTwoStep() {
    moveForward()
    moveForward()
}

func moveForwardFourStep() {
    moveForward()
    moveForward()
    moveForward()
    moveForward()
}

moveThenToggle()
turnAround()
moveForwardTwoStep()

moveThenToggle()
turnAround()
moveForwardTwoStep()

turnLeft()
moveThenToggle()
moveThenToggle()
turnAround()
moveForwardFourStep()

moveThenToggle()
moveThenToggle()
turnAround()
moveForwardFourStep()
Treasure hunt - A better solution
func moveThenToggle() {
    moveForward()
    moveForward()
    toggleSwitch()
}

func turnAround() {
    turnLeft()
    turnLeft()
}

func returnTwoStep() {
    turnAround()
    moveForward()
    moveForward()
}

func returnFourStep() {
    turnAround()
    moveForward()
    moveForward()
    moveForward()
    moveForward()
}

moveThenToggle()
returnTwoStep()

moveThenToggle()
returnTwoStep()

turnLeft()
moveThenToggle()
moveThenToggle()
returnFourStep()

// no need to return back to the center
moveThenToggle()
moveThenToggle()

See all solutions in the video below

Leave a Reply

Your email address will not be published. Required fields are marked *