What is the correct way to write a function with a parameter to say move a object "x" amount of times in a direction

In Plagrounds on the iPad you are taught to write the function like this, which makes perfect sense.


func move(steps: Int) {

for i in 1 ... steps {

moveForward()

}

}


BUT.... the following also works without having to create a for loop.



func move(steps: Int) {

moveForward()

}


I could be wrong, I surmise that a variable could be set to find "steps" to pass to the function via the for loop, and the function without would be an enterable constant.


Is my surmise correct?


Cheers


Trent

Replies

No spaces between start of index and max for example a...b


   moves(forward: 7)
...
    func moves(forward steps:Int)  {
        for i in 1...steps {
            moveForward(on: i)
        }
    }

    func moveForward(on step:Int) {
        print("Moving forward \(step) ...")
    }


Output

Moving forward 1 ...

Moving forward 2 ...

Moving forward 3 ...

Moving forward 4 ...

Moving forward 5 ...

Moving forward 6 ...

Moving forward 7 ...