Most functions rely on parameters to perform their task.
Functions with parameters expect to receive a values.
Functions without parameters.
Do not expect to receive any values.
In this lesson, you will create a function that defines parameters.
First create a new file named Parameters Java and make sure the class has a main method.
The area of a rectangle, as you know, is equal to length times width.
You've been given three rectangles and each rectangle has different dimensions.
Our job is to calculate the area of each rectangle.
How will we do this?
Step one is to create a function that expects to receive a length and a width.
Here I have a function called calculate area.
The function expects to receive two double values a length and a width.
A parameter store is a value that your function expects to receive.
The function calculate area defines two parameters.
Therefore it expects to receive two values.
The next step is to call the function by passing in an equal number of values.
In order to call the function calculate area, you need to pass in one value for every parameter that
it defines.
A value that you pass into a function is known as an argument.
The function calculate area defines to double parameters.
Each parameter expects to receive a double value.
So in order to call the function, you need to pass in to double values.
The actual values that we pass in are called arguments.
In step three, the function uses its parameters to perform a task.
The function calculate area uses its parameters to calculate an area.
If we were to trace the runtime first we're calling calculate area.
The values that we pass into the function are arguments.
When the function gets invoked, the first parameter stores the first value that was passed, then the
second parameter stores the second value that was passed in.
The function uses the value inside each parameter to perform its task.
It calculates an area and prints it.
You might be wondering why is this beneficial?
Think about it.
If you want to calculate the area for three different rectangles, I can just call this function three
different times each time passing in a different set of arguments.
If I call the function and pass in these values, the first parameter stores the first value that was
passed, then the second parameter stores, the second value that was passed in the function uses the
value inside each parameter to calculate an area, and it follows that the function can calculate an
area for any pair of values that get passed in.
The function is completely reusable.
Let's do this in code, starting with step one, creating a function that expects to receive a length
and a width.
All right.
Our function is going to be publicly accessible.
It will be void.
It's not going to return any values.
We'll talk about return values in the next video.
And we're going to call the function calculate area.
Our function calculate area expects to receive two values.
So we need to define two parameters double length and double width.
Think of a parameter as a variable that just stores whatever value gets passed into our function.
So a double length will store the first value that gets passed in and double width will store the second
value that gets passed in.
Task two is to call the function by simply passing in two values.
I'm going to call the function calculate area.
And the reason this is not auto completing is you'll remember the function calculate area needs to be
static as well in order for it to be called from the static main function.
Don't worry about what static does.
We will talk more about static in module two object oriented programming.
For now, just know that we need to have it in order to call this function for main.
And now I'm going to calculate the area for a rectangle with a length of 2.3 and a width of 3.6.
The values that I'm passing in are called arguments.
The variables that will end up storing these values are called parameters.
It's really important that you make that distinction.
In step number three, the function is going to use its parameters to perform a task.
So when this function is called, it's going to receive two values where each value gets stored inside
of a parameter.
And now we can use the parameter values to calculate an area.
Double area is equal to length times width.
And after we calculate the area, we can print it.
System, dot, dot, print line.
The area is equal to the area that we calculated and we are done.
And now, instead of running our code the normal way, we will visualize the runtime as we've been doing
in the workbook solutions.
So I'm going to put a breakpoint here and that's it.
And now you'll remember that for this to work, for us to be able to visualize the runtime, you need
to launch the exact folder that contains your Java file, in this case, section four.
And now we can press debug to visualize the runtime.
And in order to step inside the function that I'm calling, I need to press the button, step into.
All right, so I'm calling the function calculate area, passing in two values, passing in two arguments.
The first parameter length stores, the first value that was passed in the second parameter stores,
the second value that gets passed in.
So now length equals 2.3 and width equals 3.6.
And to further prove to you that parameters are essentially just variables, press this button and you
can visualize the length variable and the width of variable.
And now we can use the values inside of each parameter, the values inside of each variable to calculate
an area for a rectangle with a length of 2.3 and a width of 3.6.
And the area for such a rectangle is 8.28.
Once you reach this line, do not press, step in to press, continue to resume execution naturally.
And the area for a rectangle with such dimensions is 8.28.
What's beautiful about this is that the function is reusable.
I can call it as many times as I want to calculate the area for any rectangle that I want.
I can calculate the area for a rectangle with a length of 1.6 and a width of 2.4 for a length of 2.6
and a width of 4.2.
It doesn't matter.
I can reuse the function as many times as I want to, and this is a really, really powerful way of
coding.
All right, let us visualize the runtime once more.
So here I'm calling the function calculate area and line number three.
I'm going to step inside the function that we're calling the length parameter stores.
The first value that was passed in the width parameter stores, the second value that was passed in.
I will keep stepping into the function.
The area based on the values that were passed in is equal to 8.28 and now do not make the mistake of
pressing step into because then it's going to step into the print line function, which we don't want
to do press step over to run this line.
And the area is 8.28.
And now if I want to continue to the next the breakpoint, I can press the continue button.
All right.
Now I'm calling the function again, passing in a value of 1.6 and another argument that equals 2.4,
stepping into the function that we're calling the first parameter stores, the first value that was
passed in the second parameter store is the second value that was passed in.
And now we're calculating an area based on the parameter values.
If I keep stepping into area equals 3.84.
Don't make the mistake of pressing step in because then it's going to step into the print line function,
which we don't want to do.
We will step over this line and it prints area is equal to 3.8 for continuing to the next breakpoint.
Stepping into the function again, the length parameter equals 2.6, the width parameter equals 4.2.
We calculate an area based on these parameter values and then we can print the area.
10.92.
And now if I press continue because there are no other break points that come after the run, time will
just terminate.
Beautiful.
So to recap, a parameter store is a value that your function expects to receive.
When a function defines parameters, then in order to call it, you need to pass in a matching number
of arguments based on the position of the parameter.
It's going to store the value that gets passed in, and then we can use these values to perform our
task.