The goal of this level is to write a function to move forward a certain number of times.

In this puzzle, a new function lets you move across multiple tiles using a single command, reducing the repetition in your code. Using a parameter, you'll specify an input (distance) for your function. When you call the function, you'll pass in a value, or argument, for distance. For example, in move(distance: 6), 6 is the argument.

The function declaration for move is provided below with a distance parameter. Use the distance value in the function to specify how many times to run moveForward(). When you call move, pass in the argument for distance to run moveForward() that number of times.

1. Fill in the function definition, using the distance parameter in a loop that calls moveForward() a given number of times.

2. If you use a for loop, make distance the number of times the loop runs. Example: for i in 1 ... distance {

3. Solve the puzzle using the move function.

Hint: First, define the move function. In the function, write a for loop that uses the distance value to determine how many times the loop runs. Your for loop could start off like this: for i in 1 ... distance. After you write your move function, call it and pass in an argument to move your character forward by number of tiles defined for distance. For example, move(distance: 5) will move your character forward 5 tiles,

The complete answer and solution is shown below:

You're getting really good at this! Defining a function using parameters can be extremely useful. It can make the function more reusable, meaning you can call it in many more situations. See how else you can use parameters!

Next lesson we will be looking at Generalizing a Function.

In this puzzle, a new function lets you move across multiple tiles using a single command, reducing the repetition in your code. Using a parameter, you'll specify an input (distance) for your function. When you call the function, you'll pass in a value, or argument, for distance. For example, in move(distance: 6), 6 is the argument.

The function declaration for move is provided below with a distance parameter. Use the distance value in the function to specify how many times to run moveForward(). When you call move, pass in the argument for distance to run moveForward() that number of times.

1. Fill in the function definition, using the distance parameter in a loop that calls moveForward() a given number of times.

2. If you use a for loop, make distance the number of times the loop runs. Example: for i in 1 ... distance {

3. Solve the puzzle using the move function.

Hint: First, define the move function. In the function, write a for loop that uses the distance value to determine how many times the loop runs. Your for loop could start off like this: for i in 1 ... distance. After you write your move function, call it and pass in an argument to move your character forward by number of tiles defined for distance. For example, move(distance: 5) will move your character forward 5 tiles,

The complete answer and solution is shown below:

Code:

let expert = Expert() func move(distance: Int) { for i in 1...distance { expert.moveForward() } } move(distance: 6) expert.turnRIght() expert.move(distance: 2) expert.turnRIght() move(distance: 5) expert.turnLeft() move(distance: 5) expert.turnLeft() expert.turnLockUp() expert.turnLeft() move(distance: 3) expert.turnRight() move(distance: 3) expert.turnRight() move(distance: 4) expert.collectGem()

Next lesson we will be looking at Generalizing a Function.

## Comment