- So we've learned how
to use variables, loops,
and arrays to create different
behaviour in our code.
But so far, everything
we've done has been written
from top to bottom.
For pretty much all of our programmes,
we've created a few variables,
gathered some input,
done something with it
and spit out a result.
And while you can get
a long way with this,
programming doesn't start
to get really interesting
before we're able to reuse code.
And this is exactly where methods come in.
A method allows us to group together code
that performs a specific task.
This way we can write the code only once
and then use it whenever we need.
But first this video is
sponsored by Milanote.
Milanote is a tool for
organising your creative projects
into free form visual boards.
It can be used for any creative project
but it's particularly well
suited for the early stages
of game development.
In fact, Milanote just
released a bunch of new updates
including support for code snippets,
making it easier than ever to collaborate
and share ideas for a project
with your fellow developers.
It's available for both
desktop, Android, and iOS,
which is very convenient as you never know
when the next idea might strike.
We actually use Milanote
ourselves for a project.
So if you want to have a closer
look at Milanote in action.
Definitely check out that video.
Finally Milanote is free.
So get organised and start collaborating
with your friends now by
simply clicking the link
in the description.
Say we want some code that prints
a random number in our console.
Well we can create a
method that does this.
And there are two things
that we need to do
when working with methods.
First we define the method
and then we call it.
And what does that mean?
Well I like to think of
a method like a machine.
When we define a method,
we choose a name and what
we would like it to do.
Here we are creating the machine
and telling it how to behave.
In this case, we want to create
a machine called PrintNumber
and give it some code for
displaying a random number.
Let's have a look at doing this in C#.
So far, we've written all
of our code inside of Main.
But when we want to define a
method, we do this outside.
It doesn't matter if it's before or after
as long as it's not inside of Main.
So to define our method, we write void,
followed by the name,
in our case PrintNumber,
then we open and close two parentheses,
and then some curly brackets.
And inside the curly brackets,
we write what we want our method to do.
In this case, we want to create
a random number generator,
get a random number from it,
and write out the result.
And that's it for defining our method.
But if we run our programme
now, nothing would happen
because we also need to call our method.
Calling a method means telling
the code inside to execute.
You can think of it like
we're turning on the machine.
To do this, we choose to place in our code
where we would like to call the method.
I would like this to run as
soon as the programme starts.
So I'll do this on the
first line inside of Main,
here we write the name of the method,
followed by some
parentheses and a semicolon.
And that's it.
If we run our programme now,
it's going to see that we want
to call the PrintNumber method,
go inside of that method,
execute all the code inside,
and the result is that
we get a random number.
And we can of course choose
to call this anywhere
and as many times we'd like.
I'm just going to call it
a few times in a row here.
And as you can see, we now
get three random numbers
by reusing this same piece of code.
Now some of you might have noticed
that the method we've created here
looks a whole lot like Main.
That's because Main is also just a method
but it's special because
we don't need to call it.
Main is just always called
right when the programme starts.
Also note that methods are
often referred to as functions.
In C#, these two terms are the same.
So if you hear me saying
function, don't freak out.
It's the same.
All right, with that explanation,
let's try creating some
methods for ourselves.
So let's imagine that we are
creating a speed dating programme
where the user gets to
meet a bunch of new people.
But instead of meeting other humans,
which would be pretty boring,
let's have the user meet aliens instead.
In fact, let's make a
method that creates an alien
with a random name and age,
and have it say "Hi" to the user.
So as we learned to create a
method we go outside of Main,
that would be down here.
It's still within the
class but outside of Main
and here we start by writing void,
then the name of our method,
in my case I'm going to call it MeetAlien.
I'm going to open and
close some parentheses
and some curly brackets.
I'm also going to take
this curly bracket here
and put it down here, just
like I do with if statements,
just to make it easier to read.
And that's it.
We've actually now defined this function
but we should of course
put some codes inside
to have it do something.
I'm going to start out by creating a name
and an age for our alien
and I would like to base
this on random numbers.
So first off, we need to create
a random number generator.
So we'll go random, we'll
call our generator numberGen
and set it equal to a new random.
Again, we've done this in previous videos.
The syntax for this is still a bit weird
so just write after me.
And now we can use this number generator
to get random numbers.
So if we want to create
a name for our alien,
we can go string name, and
we can set it equal to,
and I want all my aliens to
have a pretty weird name.
I want them to be called
something like X, dash,
and then some kind of random number.
So I'm going to do X, dash,
and then plus numberGen.Next
in order to create a
random number between,
and I'm just going to
put in 10 and then 9,999.
That should look like an alien name.
Then for the age, I'm going to
do pretty much the same thing.
I'm going to create an integer called age,
and set it equal to a random
number, so numberGen.Next
between 10 and 500.
Let's pretend these aliens get pretty old.
And then we can print some of this
information to the screen,
so we can go Console.WriteLine,
"Hi, I'm, and then the name."
Let's do another one, Console.WriteLine,
"I'm and then age, years old."
And let's do a third one,
"Oh, and I'm an alien."
So I do admit that this
method is a bit weird
but it should in fact work.
The only thing that we
need to make sure to do
in this case is to add a static keyword
in front of our method.
Now what do I mean by this?
The only thing that we need
to do to make this work
is go to the top of our
method here before the void
and add the static keyword.
As you can see the main
function has this as well.
We'll talk about why in the next video
where we're diving into classes
but for now just make sure to include it
or you'll get an error in your code.
And that's it for our function definition.
Now we can call it wherever
we'd like in our code.
I'm just going to go to Main
here and try calling it here.
So let's go ahead and meet an alien
right when the programme starts.
And if we run this now,
we can see that it says,
"Hi, I'm X-8203.
I'm 154 years old.
Oh, and I'm an alien."
Pretty cool.
And of course what's
happening in our code here,
is that we're going to our main function
which gets called right at the
beginning of our programme.
It sees that we want to call MeetAlien,
so it goes down here, into
our MeetAlien function,
and executes all of this code
before going back and continuing down.
So if you want,
we can go ahead and call
MeetAlien some more times
in order to meet some different aliens.
And I'm just going to put some
kind of spacing in between here.
So Console.WriteLine and I'm
just going to put in a few dashes
just to clearly separate the
two aliens that we're meeting.
And if you run this programme again,
we can see that we're now
meeting two different aliens
with a different age and a different name.
Awesome!
So that's the most basic
example of a method.
But where methods really start to shine
is when we add parameters
and return values.
But what does that mean exactly?
Well so far,
our machine has been pretty
closed around itself.
We can run it.
It does some things and
that's pretty much it.
But imagine if we were
able to give the machine
some input when we run
it that it can then use.
A good example of this
is if we want to create a
method that squares a number.
In this case, we probably want
to be able to input a number
that the machine should then square.
This is called a parameter.
And it's pretty simple to write.
When defining our method,
we go inside of the
parentheses and write the type,
in our case that's an integer,
followed by the name of the parameter,
I'm just going to call this number,
then inside the curly brackets,
we can use this as any other variable.
In this case, I'm going to calculate
the result by multiplying
the number with itself.
So we've now created a machine
that takes in an integer as a parameter.
When we then call the
method and run the machine,
we just pass in whatever
number we'd like it to use.
Of course it would be cool if the machine
could also spit out the result.
We call this a return value.
In our case, we would
like to return the result
of our calculation which is an integer.
To do this we replace void,
which means that we don't want
to return anything, with int.
Then inside the curly brackets
when we've done everything
we wanted to do,
we can end the method by writing return
followed by the value
we would like to output.
In our case, that is the result.
And this allows us when calling the method
to get a value back.
And we can of course store
this in another variable
and use it for whatever we'd like.
So we're creating a machine called Square
that takes in an integer
as a parameter, squares it,
and outputs the result.
Let's have some fun with
this in various code.
So let's say that we
wanted to create a method
that multiplies to numbers.
Well in this case, we would
again go outside of main here
and we would type void to create a method.
We give it the name which
is something like Multiply.
Open and close the parentheses
and the curly brackets.
And if we want this method
to multiply to numbers,
we need a way to input the two
numbers that we would like.
So we'll add these as parameters.
To do this, we go inside
of our parentheses.
And here we first put the
type of what we want to input.
So in this case that would be an integer
and I'm just going to call
it num one, for number one.
And the cool thing is that we
can actually allow ourselves
to input more than one value.
So if I just write a comma here,
we can create another parameter.
So this is going to be an integer as one,
and I'm going to call it number two.
And there we go, and now
we can actually do things
with these two numbers
inside of our method.
So in this case, we probably
want to calculate the result
and store it in a variable.
So let's create an integer called result
and set it equal to number one
multiplied with number two.
It's actually that easy.
And if you want, we could of course
just print out the results.
So we could do Console.WriteLine,
the result is,
and then result.
There we go.
Of course just like we did earlier,
we need to add the static keyword
in front of our method here.
For now you need to do this
to all of your methods.
Again, we'll talk about
why in the next video.
And with that, we have
to find our function.
And so we can go up to
main here and call it.
So I'm going to type in Multiply,
and then inside of the parentheses
we can put in a few numbers.
I'm also going to put
in the number 3 and 8.
I'm also going to do one with 7 and 13,
and let's do one with 11 and 5.
Cool.
And if we now run this,
we can see that the
result of 3 times 8 is 24,
the result of 7 times 13 is 91,
and the result of 11 times 5 is 55.
Awesome, but right now
we're just displaying
the result in the console.
What if we wanted to multiply two numbers
and then use the result to do something?
Perhaps we could check if the result
is an even number or an uneven number.
So to do this, we add a return value.
I'm just going to get rid of some
of the multiply function codes here,
and down here instead of writing a void
which means again, return nothing.
We write the type of the number
that we want to to return.
In our case that's an integer
because we want to return
the result variable.
Then instead of printing
this directly to the console,
let's simply write return,
result, and then a semicolon.
And now up here what we
call the multiply function,
we can now store this
result in the variable.
So we can create some kind of integer,
we can call it result and set it equal to
whatever is outputted
from a multiply function.
And please note that while
I am giving the same name
to this variable and this
one, you don't have to.
You could easily call this one
answer or anything else
that you would like.
I just think it makes
sense to call it result.
And now we can do things with this.
So we can print it out, Console.WriteLine,
the result is, and then the result,
and we can check if it's an even number.
So we can go if, result,
and a pretty cool way to check
if a number is even or not
is to divide by two and see if it adds up.
So just to give an example of
this, if we divide two by two,
that gives a whole number
and so two is an even number.
The same thing happens if
we divide say, eight by two,
that is going to be four.
Again a whole number and
so that number is even.
However, if we divide say, three by two,
this is going to give one and a half.
This means that there is a
remainder to this division,
and we're getting a decimal number.
So this is uneven.
And the same thing with five and so on.
Again five divided by two
is 2.5, this is also uneven.
So we can use this to check
if a number is even or not
by checking if the remainder
of the division is zero.
So in this case, the
remainder is definitely zero.
The same thing here but
if we divide three by two,
the remainder is .5
and the same thing if
we divide five by two,
the remainder is also .5.
To do this in C#,
we use the modules operator
or the percentage sign.
So here we're saying if we
divide the result by two,
what is the remainder?
And we need that remainder
to be equal to zero
in order for the number to be even.
So if that is the case, if this is true.
If the remainder of the division is zero,
then the number is even.
And so we can go Console.WriteLine,
result, is an even number
If not, so else, then
the number is uneven.
So Console.WriteLine,
result is an uneven number
and there we go.
If we now run this, we
can see that the result
of three times eight is 24.
That's correct and 24 is
indeed an even number.
And if we do this again
with some other number,
let's say three times three,
it says here the result is nine
and nine is an uneven number.
Awesome.
So that's the perfect
example of how we can create
a function that takes in some input.
In our case two numbers
here, does something with it,
and returns a result when simply displayed
or we can do different things
depending on the result.
Awesome.
Now for this week's challenge,
I want you to create a method
that can count the number
of words in a sentence.
To do this, the method must
take in a string, the sentence.
Figure out how many words
are in that sentence
and return that number as an integer.
So as you can see my programme here,
asks the user to enter a sentence.
I'm just going to put
in, "I love programming."
I'm going to hit enter,
and it now says that there are
three words in that sentence.
And indeed there is.
"I love programming."
Awesome!
Now to do this, we need to
know how to get the word count.
And we can do that using
the dot split method.
As you can see, I have
an example string here.
And the split method is going
to split this string in two
in all places that has
a specific character.
So if I go example.Split,
I can then open and close
parentheses and a semicolon.
And then inside here, we
can put two apostrophes
and this allows us to input a character.
In our case, we want to split
every time there is a space
so I'm just going to put a space here.
Now what this effectively does
is divide our one string here
into an array of strings.
In this case, it would be
an array of three items.
I, love, and programming
and because of that, we
can simply use the .Length
to get the number of
elements inside the array
and that should be your word count.
So good luck and as always my solution
is available on the practise forum.
That's of course a link for
that in the description.
That's pretty much it for this video.
If you enjoyed it, make sure to subscribe
and ring that notification bell
so you don't miss the next one.
Also don't forget to check out Milanote.
Again, it's free and a great
way to organise your projects
and share your ideas with friends.
To get started, sync the
encrypted link in the description.
On that, thanks for watching
and I will see you in the next video.
Thanks to all the awesome
Patreon supporters
who donated in July and a
special thanks to Dante_Sam,
Lost to Violence, Loved
Forever, NiftyliuS, Scott McKee,
faisal marafie, Replica Stuios,
Leo Lesetre, Nubby Ninja,
Jason Uritescu, Piano
Sathornlak, bobby reynolds,
Donatien Gascoin, Marc-Antoine
Girard, Jacob Sanford,
Michail Korobov, Naoki Iwasaki,
Gregory Pierce, Owen Cooper,
TheMightyZeus, Alison the Fierce, Erasmus,
I love Brackeys aw, SiriusWolf,
Fred Mastro, Hassan Sher,
Storm Daniels, and dennis solomon.
You guys rock!
