Chapter: Functions

This chapter is about the introduction to functions. This is where Swift Playgrounds really starts to get fun.

To make it as easy as possible to get started, only the pure functions without function parameters or return values are needed.

In this chapter, you basically don’t need to solve all the puzzles to understand how functions work. Nevertheless, it is recommended to solve all puzzles to get more programming practice. In addition, the beginner also learns that you can call other functions from one function. For a beginner - like a child - this aspect is indeed not self-evident.

Our verdict

Must-read for everyone.

1. Composing a New Behavior

In this puzzle we have found the first small “real” challenge. How can we turn Byte - our hero - to the right? So far we have always used the turnLeft() function to turn him to the left. A turnRight() function was missing until now. My daughter had to think for a little moment and realized that she had to call the turnLeft() function 3x in a row to turn Byte to the right.

The developers of Playgrounds have thought this through. Great.

challenge-issuing-commands

Challenge solution code: Composing a New Behavior

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// MIT License (MIT) Copyright (c) 2021 Rosi Regner
// Challenge: Composing a New Behavior

moveForward()
moveForward()
moveForward()
// turn right: Here we call turnLeft() 3 times in a row, then Byte turns to the right.
turnLeft()
turnLeft()
turnLeft()
// end turn right 
moveForward()
moveForward()
moveForward()
collectGem()

2. Creating a New Function

From here on, functions were used for the first time. Their notation was already something special. Especially the fact that the function body must always be written in curly brackets and only the statements inside these brackets belong to it.

I was asked questions like: What are functions and why do you need them at all? You can do everything without them!

Here you have to spend some patience and time to explain the meaning of functions to a child. But this should not be a big problem if you present the length of the code to the child as justification: once without the function turnRight() and once with it.

Writing functions was also not a problem because of the available function snippet.

challenge-issuing-commands

Challenge solution code: Creating a New Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// MIT License (MIT) Copyright (c) 2021 Rosi Regner
// Challenge: Creating a New Function

// Turn Byte to the right.
func turnRight() {
    turnLeft()
    turnLeft()
    turnLeft()
}

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

3. Collect, Toggle, Repeat

For this challenge, Rosi decided to follow the path in reverse order - so please don’t be surprised.

challenge-issuing-commands

Challenge solution code: Collect, Toggle, Repeat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// MIT License (MIT) Copyright (c) 2021 Rosi Regner
// Challenge: Collect, Toggle, Repeat

// Byte turns on a switch, goes ahead and collects the gem. 
func switchAndCollect() {
    toggleSwitch()
    moveForward()
    collectGem()
}

turnLeft()
moveForward()
moveForward()
switchAndCollect()
moveForward()
turnRight()
moveForward()
switchAndCollect()
moveForward()
turnRight()
moveForward()
moveForward()
switchAndCollect()
moveForward()
turnRight()
moveForward()
// Call switchAndCollect() func
switchAndCollect()

4. Across the Board

challenge-issuing-commands

Challenge solution code: Across the Board

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// MIT License (MIT) Copyright (c) 2021 Rosi Regner
// Challenge: Across the Board

func collect2xAndMove() {
    collectGem()
    moveForward()
    collectGem()
    moveForward(
}

// Collect 2 gems in the first row.
collect2xAndMove()

// Collect and move to the next line.
collectGem()
turnRight()
moveForward()
turnRight()

// Collect 2 gems in the second row.
collect2xAndMove()

// Collect and move to the next line.
collectGem()
turnLeft()
moveForward()
turnLeft()

// Collect 2 gems in the third row.
collect2xAndMove()

collectGem()

5. Nesting Patterns

challenge-issuing-commands

Challenge solution code: Nesting Patterns

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// MIT License (MIT) Copyright (c) 2021 Rosi Regner
// Challenge: Nesting Patterns

// Byte should turn over completely.
func turnAround() {
    turnLeft()
    turnLeft()
}

// Byte climbs the stairs, collects the gem and returns to the starting position.
func solveStair() {
    turnAround()
    moveForward()
    collectGem()
    turnAround()
    moveForward() 
}

solveStair()
turnAround()
solveStair()
turnRight()
solveStair()
turnAround()
solveStair()

6. Slotted Stairways

challenge-issuing-commands

Challenge solution code: Slotted Stairways

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// MIT License (MIT) Copyright (c) 2021 Rosi Regner
// Challenge: Slotted Stairways

// Collect gems in a column.
func collectGemTurnAround() {
    moveForward()
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    moveForward()
}

// Main function.
func solveRow() {
    collectGemTurnAround()
    collectGemTurnAround()
    turnRightAndMove()
    collectGemTurnAround()
    collectGemTurnAround()
    turnLeftAndMove()
    collectGemTurnAround()
    collectGemTurnAround()
}

// Switch to the next column (right).
func turnRightAndMove() {
    turnRight()
    moveForward()
    turnRight()
}

// Switch to the next column (left).
func turnLeftAndMove() {
    turnLeft()
    moveForward()
    turnLeft()
}

// Call the main function.
solveRow()

7. Treasure Hunt

challenge-issuing-commands

Challenge solution code: Treasure Hunt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// MIT License (MIT) Copyright (c) 2021 Rosi Regner
// Challenge: Treasure Hunt

// As the name suggests: Move-Toggle-And-Return.
func moveToggleAndReturn() {
    moveForward()
    moveForward()
    toggleSwitch()
    turnAround()
    moveForward()
    moveForward()
}

// Byte should turn over completely.
func turnAround() {
    turnLeft()
    turnLeft()
}

// As the name suggests: Move-Toggle-Toggle-And-Return.
func moveToggleToggleAndReturn() {
    moveForward()
    moveForward()
    toggleSwitch()
    moveForward()
    moveForward()
    toggleSwitch()
    turnAround()
    moveForward()
    moveForward()
    moveForward()
    moveForward()
}

moveToggleAndReturn()
moveToggleAndReturn()
turnRight()
moveToggleToggleAndReturn()
moveToggleToggleAndReturn()

Read next: Learn to Code 1 - Chapter For Loops