We have situations, one, we have to do things repeatedly for that we have loops.
All right.
But we would like to run code based on different conditions, but we can use controls for that.
And we use methods if we would like to wrap these into a block and reuse it for different purposes.
To explain what methods are, I'm going to use an example that my math teacher gave me in primary school,
maybe you have seen something similar.
So we had imaginary machine in primary school.
The machine had one input and one output.
And we had to figure out what the machine does.
For example, an input like five and we had an output 15.
OK, so what the machine does, it wasn't obvious the time, but the machine multiplies the number that
goes and by free and the result comes out.
We also had exercises when the machine had two inputs and one output.
Five and Fragos and 15 comes out.
And the machine just simply multiplies the two numbers.
The good thing about these machines is that they can be reused with other numbers and they can just
do their job.
In Java, you can have machines or methods that have a lot of inputs or machines that don't have an
input or output, it just prints out a text two times or it has an input, but there is no output more
than printing out something.
By definition, a method or a function is a block of code, which only runs when it is called.
Functions are one of the most important features of all programming languages.
It is very important to understand them and use them the correct way.
The takeaway from here is that a method is a block of code that runs only when it is called.
They have inputs and outputs, if there is any.
And their biggest advantage is that they are reusable.
So what we are going to cover in this lecture, first, we are going to look into how to create a method,
then how to call a method.
After that, I will tell you more about parameters and arguments.
What are the differences then?
Return values.
And finally, I will show you what the function overloading is and how to do that after this lecture.
That will be according to.
All right, let's do this.
OK, so how we can define a method, first and foremost, it must be declared within a class, the most
basic way to define a method is that to start with the day to type, that the function returns, then
the function name next parentheses.
After that, curly braces with a return statement.
The returned values data type must match with the return type.
The return type can be void, which means that the function doesn't return anything.
In that case, we don't need the return statement, just like in the following example.
This say good morning function simply prints out.
Good morning.
Function definition can be more complex than this, you can add more parameters to definition as modifiers
can be public, private or package.
We will cover these later in discourse.
Static shows Javor that the function belongs to the class instead of an object.
This is also a topic that I will tell you more after you will have knowledge and classes and objects.
The return type and the function name have been mentioned previously.
Parameters are optional, parameters are passed and can be used in the functions block.
You have already used such functions.
For example, the main method.
The public is an access modifier, static shows that it belongs to the class well.
It shows that it doesn't return a value made is the function name, then a parentheses string square
brackets shows that it is an array of strings and it is named ARGs.
Now, let's see a function that's a bit less complex, get 10 percent of discount price, takes an enterprise
and multiply it by zero point nine and returns the result because we multiply a number by zero point
nine, we will get a nonpoor number.
So the return data type is double.
One additional thing I would recommend you is that the function should do one thing and its name must
tell what the function really does.
To call a method in Java, right, the methods name first, followed by two parentheses with the parameters,
if there is any, and a semicolon in the case of get ten percent of discount price, we can use directly
twenty thousand as an input or we could use a variable name like Price.
There are other ways to use it, like a sign the output or the return value to a variable, as you can
see, the variables data type matches the functions return data type.
You can also use the function as a parameter for any other functions, like, in our case, for Quintel
and function.
Now let's move on to parameters and arguments.
Information can be passed to methods, true parameters, they are specified after the functions name
inside the parentheses and separated with commas, parameters act as variables inside the methods.
A method can have an unlimited amount of parameters, but I suggest you not to have more than three,
the following example has a method that takes to end as parameters and returns the discount percentage
of the price.
Now, let's see what's the difference between parameters and arguments.
We have a defined function here and a case where we call the function when a parameter is passed to
the method and is called an argument.
So from the example here and price is a parameter, while twenty thousand is an argument.
Know that when you're working with multiple parameters, the method call must have the same number of
arguments as there are parameters and the arguments must be passed in the same order.
Moving on to return values, when you use a function to return a value, the return type must match
with the data type of the value that return.
That data type can be anything a primitive or an object.
When your function doesn't return a value, the function must use void instead of a return type.
Additionally, there is no return statement in the function like in the case of say good morning function.
Our last topic is overloading with method, overloading, multiply methods can have the same name with
different parameters.
Why would we need that?
Because we can have functions that do the same thing on different data types.
And instead of using different names for the same purpose, we use overloading.
OK, this go template for overloading may not make it clear.
So move on to the examples.
So here we have the same functions, same definition, same return statement, but the parameters are
different with having just the end version.
We couldn't use double or float values or we should convert them into an end by having a function with
a double parameter.
We can use decimal numbers with the same name based on the data type we pass to the function.
Java will know which functions should be called.
So that's it, I hope it was clear if it wasn't, I will try to make it more understandable in the coding
part.