Welcome back.
In this video we're going to do two things.
Number one we're going to use two parameters instead of one.
And number two we're going to have a return type.
So instead of just having these very simple examples where we write something or rewrite something specific,
let's have actually a useful application.
So the useful application will be an adder okay.
Where a Subtractor subtractor.
So we can just go ahead and create a method.
Let's just start with the default approach that we had before.
Add to values.
And I'm just going to add that I define in here as parameters.
So parameter one one will be an int.
And I'm going to call this one value one.
And then parameter two will be an int that I'm going to call value two.
So you see if you want to use multiple parameters you are separating them with a comma.
So you're saying okay there will be the first value and then there will be the second value.
And what type will those two values have.
They will be of type int.
They have to be of type int.
Because what I want to do next is I want to add them together.
So I'm going to say int result is going to be value one plus value two.
Now how do I get this result out of my method.
Add two values.
Now I can obviously just use Console.WriteLine and say result is.
And then I can just write the result.
That is fine and that will work.
If I now go ahead and use add two values here.
And I want to call this method.
Now obviously I need to pass two values.
Let's say I want to pass five and ten.
And then I call this method.
And let's run this just to see.
So it says the result is 15.
Very basic right.
So it's just adding two values together then getting the result and then writing the result onto the
console.
However, what if this add two values really should only add them together and return the result, but
never actually write it because writing it we do outside.
So this method really has one and only one responsibility, and that is to add two values.
So it's not responsible of writing it out or whatever.
Okay.
So in order to write something out we can use another method.
Or we can just use the console write line method.
So now how do I get this result out of here.
Well I can use the return keyword.
I can return the result.
However, I can only return the result from a method if the method indicates that it will return a very
specific type.
So instead of returning a void, I now need to say that I'm going to return an int because that's what
the result is of.
So the general structure here is you define the data type here just before the name of the variable.
Then inside of your brackets you define which parameters you want to have.
And then you do whatever magic you want to do within those curly brackets.
And after that you can go ahead and return that.
Or you need to return that.
Because if you don't return anything, if this line now doesn't exist, then our method will break.
It will basically say not all code paths return a value.
So this just means that we need to use this return keyword and return the same data type as here.
So this is an int and result needs to be an int.
Just to show you if I made this a double for example, then this would cause an error here because suddenly
result is a double.
But I well I promise that I would return an int.
So I need to make sure that whatever I return has the same data type as what I defined here.
So now this here will basically be the same thing as five plus ten.
So this will return 15.
And we can store that in a variable.
So we can have another variable which will be my result for example.
And that will now store whatever the result of this calculation is.
And now I can go ahead and write the result out.
So the result is my result.
And here plus my result okay.
So this is obviously a lot more complicated than what we had before when we wanted to add two things
together.
But this is just the simplest way to explain these concepts.
They are used in significantly more complicated fashions.
But you need to understand this before you can understand the more complicated stuff.
And we're going to go over all of it step by step.
Now, while we see that now there is this method that we created ourselves, add two values that adds
two values together that we define here as parameters.
Now we're giving it as arguments here.
And we're getting or returning an int.
So if you hover over your method that we just created, it says add two values takes in an int and another
int and it returns it.
As an int.
So now this whatever returns is stored inside of this variable.
And now we use this variable in order to write it out.
Now the cool thing is if you hover over your write line method, you can see that this Writeline method
returns nothing.
It returns void.
So if you took the values from the user.
So let's say we now take the value from the user.
We're going to create a two new variables.
So num one is going to be console dot read line.
So we're taking whatever the user entered.
And we need to parse that.
So we're going to parse whatever the user entered into an int.
We've seen that multiple times before.
Now we're going to store whatever the user entered inside of num one.
And we can now pass num one as the variable.
So now our method what it will do is it will always add ten to whatever we entered.
So let's run this program.
And it doesn't say anything because we didn't say anything to for it to say.
We didn't have any indication as to what it should say.
And you see, it says the result is 15.
Now let me run this again, maybe with a different number.
So I'm going to take 25 and it says the result is 35.
So we could say something like enter a number, I'll add ten to it.
And then we enter the number.
And that number is converted into an int.
It's stored inside of this int variable num one.
We're passing that int variable num one to add two values.
Now obviously we could ask for the second value as well.
And then we could pass that second value in here as well.
Now you see that here I call it value.
And here I pass the name num one.
Those names they can be whatever.
So this name can be a very different name.
Obviously it's good if you have descriptive variable names.
But this variable here, this parameter variable is only available inside of here.
So I cannot use value one here okay.
I cannot just say value one is going to be five for example.
That doesn't work because this value one int and this value two int they're only available in the context
of the curly brackets.
But let's look at that in the next video a little more.
So see you there.