Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Functions take inputs, perform an operation on them and return an output. In this video we take a look at methods  functions that are associated with an object and how they work on the values associated with the properties of the object.

0:00
You might not be convinced yet that a struck

0:03
offers any real advantage over the tuplebased weight we tried earlier.

0:08
So far, it just seems like extra syntax to get the convenient dot notation

0:12
property accessors.

0:14
This is where it starts to get interesting.

0:17
When you work with data, you don't want to just store it.

0:20
You might want to manipulate it and get further information out of it and so on.

0:25
This is what functions are for.

0:27
With a function you can provide some inputs, manipulate the inputs,

0:31
and output some data.

0:33
With an object,

0:34
the functions that were on the data it contains are part of the object itself.

0:40
This makes it very easy to understand what the scope of the object is, and

0:44
what we can do with its associated data.

0:48
Let's take a look at our map again and overlay a grid on top of it.

0:52
Let's say here at point 2,2 we place a tower.

0:57
This tower that we haven't modeled just yet,

1:00
let's assume that it can only fire at the points around it.

1:04
So its range is one coordinate point away.

1:08
Given some enemy we can only fire at this enemy

1:12
if the enemy is within the tower's range.

1:15
So given the position a tower occupies

1:18
we need to know what are the possible coordinate points around it.

1:22
Also from the enemy's perspective, they're going to try and avoid being fired on.

1:27
So given an enemy's position, they need to know whether they are in range of a tower.

1:33
In both these scenarios, the underlying data that we need is the same.

1:37
Given a point, we would like to know what other points surround it.

1:43
We could write a function and pass in the point to determine a range.

1:47
But the advantage of objects is that we can associate a function with the data

1:51
contained inside.

1:53
Let's go back to the definition of the struct Point.

1:57
Inside the body of the function, so right after the stored properties,

2:02
let's write a function named surroundingPointsWith.

2:06
So remember the syntax for a function is we start with the word func.

2:10
We then give it a name in lower camel case, so surroundingPointsWith.

2:19
Now this function will take a single parameter of type Int

2:23
that determines the range around which we want the points for.

2:27
So for example, if the range is 1, we only want the point one space away from us.

2:33
If the range is 2, then all the points two spaces away from us and under.

2:39
So we'll say, this parameter's called range and we'll give it a type of Int.

2:45
Now remember function parameters can have external and local names.

2:49
So I'm going to give this parameter an external name withRange so

2:54
that it reads better when we call it.

2:57
If you remember function parameters can also have a default value so

3:00
we'll give this a default value of 1.

3:03
Because we said for

3:04
now we're going to assume that a tower can only fire at the points one space away.

3:09
So far so good, right?

3:11
Okay, this function is going to return a number of different points.

3:15
We learned in the functions course that we could return multiple values

3:20
using a tuple.

3:21
We can do that here.

3:23
But the problem is, we don't really know how many values we need,

3:27
since the return value totally depends on the range that we pass in.

3:32
If we pass in a range of 1, then we're asking for

3:35
around six points, more or less.

3:39
If we pass in a range value of 2, then there's more values to return.

3:43
So, rather than a tuple, we're going to return an array of point instances.

3:50
Remember, that when we have an array of strings,

3:52
the type is string enclosed in square brackets as an array of strings.

3:58
And with custom types, it's the exact same thing.

4:01
So we can specify our return type as an array of Point.

4:06
Okay, now let's open the body of the function.

4:09
So let me make a tiny bit more room here.

4:11
Okay, inside the body of this function, first,

4:15
we'll create a local array to store all these different point instances

4:19
that we get when we ask for the points in a surrounding range.

4:25
This will make more sense as we write the function.

4:28
So we'll declare a variable named results.

4:33
We'll specify the type as an array of Point.

4:36
And I'm going to assign it initial value of an empty array.

4:40
The reason this array is a variable is because when we get a particular point,

4:46
we're going to add it to this array.

4:48
And if you remember,

4:50
we can only append items to a mutable array that is created using var.

4:55
Again, we're specifying the type here because otherwise,

4:59
the compiler since we're only assigning an empty array as the initial value,

5:03
the compiler doesn't know what type of data is going to go into this array.

5:07
By specifying the type as an array of Point, we're restricting our

5:12
objects to only be of point instances so I can't add an integer or a string in here.

5:17
As you write more code, you'll figure out where it's useful to write out the type,

5:21
where you can leave it out all together, and where it's absolutely necessary.

5:25
Okay, now the next little bit of the function can get a tiny bit tricky, so

5:30
let's walk through it first before we write any code.

5:33
If we look at a point in coordinate space, let's say 2,2, and

5:38
given a range of 1, the points in the range are these points.

5:44
Let's write them out.

5:45
So we have 1,3, 2,3, 3,3, then below that,

5:49
we have 1,2, 2,2, the point we're at, 3,2,

5:53
and then finally, 1,1, 2,1, and 3,1.

5:58
If you look closely, you'll notice there's a pattern here.

6:02
Take the base point.

6:03
We have x is 2 and y is 2.

6:06
In all the values that we've specified here,

6:10
the minimum x value is 1 and the maximum x value is 3.

6:15
Similarly the y values also go from 1 to 3.

6:20
If you do the math, you'll see that there's a pattern.

6:23
The range of values for both x and y are essentially

6:26
the existing x point minus the range to existing x plus the range.

6:32
So, if we take the xrange value, which in this specific case, it is one,

6:38
and then, we match it with every value in the range of values for y.

6:43
And we know that y goes from 1 to 3.

6:46
So if we do that, we get (1,1), (1,2), and (1,3).

6:52
Then we take the next x value in the range, which is 2.

6:56
And we do this, we match that x value with all the y values from 1 to 3.

7:01
So we get (2,1), (2,2) and (2,3).

7:05
Finally, we do that for 3, and you'll notice, by doing this, we get

7:10
all the coordinate points that we want that are in range of our existing point.

7:16
We can easily write this out in code using everything we've learned so

7:19
far up to this course, simple ranges and for in loops.

7:25
Okay, first we need to iterate over the range of values in the xcoordinate.

7:30
Remember, when we were looking at this conceptually, we fixed the value of x.

7:35
And then we used the value to match it up with different values of y.

7:39
So we do for xCoordinate in.

7:44
And if you remember in a for in loop we can specify a range of values.

7:48
And for this range I'm going to specify this starting

7:53
value as (xrange)..., and then an end value of x+range.

8:00
And we'll open the body of the for in loop.

8:03
Now I put xrange and x+range in parenthesis.

8:07
So it's a bit easier to read.

8:09
Remember that there are two types of operators, the closed range and

8:12
the half open range operator.

8:14
We want x+range to be included in our range, so

8:18
we use the closed range operator.

8:21
Now, inside this for loop, I'm going to add another for loop.

8:24
But this time we're going to iterate over the ycoordinates.

8:28
So, in basically the exact same way, we'll say for

8:32
yCoord in yrange

8:37
to y+range.

8:42
The first loop iterates over the range of xcoordinates and

8:46
does some work with each.

8:48
So given our values for x,

8:50
with our first loop, we'll get a value of x as the first xcoordinate value.

8:56
Then we step inside the body of the loop where we encounter our second for loop.

9:02
This time, it's for

9:02
the ycoordinates, so we start looping through the range provided.

9:07
We'll pull that first value out, which is 1, and

9:10
then step into the body of the for loop, where we use both that x and y value.

9:16
Now, once the work is done, we then go back to the top of the second loop.

9:20
We'll get that second value for y and repeat the process.

9:24
In this way, for a particular value of x, say 1,

9:28
we pair it up with all the values in the range for y, so 1, 2, and 3.

9:34
Once we've iterated over all the values of y,

9:37
we go back to the top of the first loop.

9:40
We'll get the second value for x.

9:42
And repeat that process over.

9:44
Okay, so now inside the second loop, let's add some code.

9:48
Given that we have a value for both x and y, we can now use that

9:53
to create an instance of point using the values from our loops as arguments.

10:00
So we'll say, let coordinatePoint =, and we'll create a new instance.

10:07
Remember, the syntax for creating an instance is you start with the name

10:12
of the object, so Point and open parentheses, and

10:16
x code should autocomplete this special function for us.

10:20
We don't know what this function is doing exactly just yet, but

10:24
we use it to assign values to our stored properties.

10:27
So we'll say that the x stored property, the value for it,

10:31
should be the same as the value contained in the xcoordinate constant and

10:36
for y, the ycoordinate constant.

10:39
Okay, now we have a point instance.

10:42
We can now add this to the results array using the append function.

10:48
So we'll say results.append and we'll do (coordinatePoint).

10:53
Okay, again, let's take a step back and look at this from a high level.

10:57
So, for each value of x that we get out of the range,

11:00
we take a value of y and then we create a point instance.

11:06
We then add this instance to the array that we created earlier up over here.

11:11
We repeat that process over and over.

11:14
And once the for in loops have iterated over all the possible values for x and y,

11:19
created point instances with those values and added them to the array, we now

11:25
have an array containing all the possible positions where an enemy can be fired at.

11:30
So let's return this value.

11:32
We'll do return results.

11:37
Now make sure you add this return statement in the right spot.

11:41
If you put a curse on the left side of the brace in x code like this, and you kinda

11:46
arrow back and forth, x code should tell you where the opening brace is.

11:51
So for this one we wanna make sure it's outside the for in loop.

11:55
We're adding the return statement as soon as we exit the for in loops.

12:00
Okay, what we have here is our first method.

12:03
What's a method?

12:04
Let's find out in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up