Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Kevin Walker
Kevin Walker
2,354 Points

Where does x and y value come from?

In the video after Pasan has written everything out, including the two For Loops, where does the initial value of x and y come from? How is the loop calculating the "for xCoord in (x-range)...(x+range)" if we don't set x to, say, 0?

struct Point {

let x: Int
let y: Int

init(x: Int, y: Int) {
    self.x = x
    self.y = y

}



func surroundingPoints(withRange range: Int = 1) -> [Point] {  

    var results: [Point] = []
    for xCoord in (x-range)...(x+range) {
        for yCoord in (y-range)...(y+range) {
            let coordinatePoint = Point(x: xCoord, y: yCoord)
            results.append(coordinatePoint)
        }

    }
    return results
}

}

because it does not need to know what x is yet it will know when you pas numbers through it like so :

//when you call the Point struct Point(x: 10, y: 20)

then because of the "surroundingPoints" function built in. The point also knows the surrounding points and you can call that later i believe he will talk more about it. hope that makes some since.

1 Answer

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

The values will come when the parameters get passed from the calling function. Here the code is for defining the functions & the variable space, but not values yet.