Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Some methods return the result of their work as a return value.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's take another look at
the on map method we just wrote.
0:00
The result of these condition checks
is stored in the inBounds variable.
0:03
Then the value stored in
the inBounds variable is returned.
0:09
So if point is found to be inside
the boundaries of the map,
0:14
then the inBounds variable will be true.
0:17
And the value true will be returned
from this method to the caller.
0:19
Otherwise, if the point was found to
be outside the boundaries of the map,
0:24
this method will return false.
0:27
I should mention that we can
actually shorten this code a bit and
0:30
avoid using the inBounds
variable entirely.
0:32
The way we can do this is to return
the result of this expression right away.
0:36
Instead of first storing it in
a variable and then returning it.
0:40
We can type return right here instead and
delete the other return statement.
0:44
See how that works?
0:53
There's now one less variable in our code.
0:55
Also stringing a bunch of
operations together like this
0:58
can quickly make the code
difficult to understand.
1:01
We can alleviate some of this by splitting
the code up into multiple lines like so.
1:04
I can even indent it to line
up with the line above it.
1:11
This is possible to do
because the semicolon
1:15
marks the end of the statement of code.
1:17
So I can put this statement on
as many lines as I want and
1:19
C# still knows to treat
it as a single statement.
1:22
Deciding if and when to split statements
into multiple lines really just
1:30
depends on what you think makes the code
more readable and clearer to the reader.
1:34
I decided to put operations that
deal with the width on one line and
1:40
the height on the other line.
1:44
Indenting so that things line
up vertically can also help.
1:46
All right, we've completed this method.
1:49
Let's go back to the main method in
the game class to try out our new method.
1:52
Don't forget to save the map.cs file.
1:57
We can delete this line and declare a new
point that should be inside our map.
2:01
Our map is eight units wide and
five units high.
2:08
So let's create a point
that's in square 4, 2.
2:12
Now let's call our on map method to
determine if this point is on the map and
2:20
assign the value returned to a variable.
2:24
So I'll say bool isOnMap = map.OnMap and
2:27
then pass it to point.
2:33
Just for fun, let's call OnMap with
a point that we know isn't on the map.
2:38
Just to make sure that we
get the right answer back.
2:43
I'll overwrite the point variable
we just created with the new point.
2:46
I don't have to declare
the variable again because it's
2:50
already declared as a point up here.
2:52
I just want to assign it a new value.
2:56
The same goes for the isOnMap variable.
2:58
I intentionally chose to
make a point in square 8,
3:11
5 in order to illustrate
a common programming mistake.
3:14
At first glance, one might think
that this point is on the map.
3:19
Because the map has a width of eight and
a height of five.
3:22
However, in programming,
by convention we start counting from zero.
3:25
This means that the point on the bottom
left corner of the map is 0, 0.
3:30
And the rightmost point has an x
value of one less than the width.
3:36
The topmost point has a y value
of one less than the height.
3:42
This is called zero base counting.
3:47
This throws a lot of people off.
3:50
Occasionally even experienced
programmers get bitten
3:52
by what's called an off by one error.
3:55
Let's print out the value of
isOnMap to the Console so
3:58
we can see the result of calling OnMap.
4:01
I'll add the system namespace up here and
4:04
then we'll use Console.WriteLine
to print the value of isOnMap.
4:06
So type Console.WriteLine Is OnMap.
4:10
Copy that and do it again down here.
4:22
All right, when we run this
we should see the word true
4:28
followed by the word false
printed to the Console.
4:31
Let's open the Console and
compile and run this code.
4:35
Looks good.
4:41
When we come back we'll learn more
interesting things about methods.
4:42
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up