-
Title:
Inputs to a Method
-
Description:
-
Wait.
-
>> What's wrong?
-
>> When did this turn into
a cooking show all of a sudden?
-
>> Since we had to talk about methods?
-
>> Care to explain, Rachael Ray?
-
>> [LAUGH] Well,
have you ever baked a cake before?
-
>> I have eaten a cake before.
-
>> Okay.
-
Well, then you're fully qualified for
this.
-
>> Okay.
-
>> All you have to do to bake
a cake is to put in flour, sugar.
-
>> The ingredients.
-
Okay.
>> The ingredients.
-
Yeah.
-
And then you follow
the instructions of the recipe.
-
>> Okay.
>> And then out comes a cake.
-
>> Like this?
-
>> Yeah.
-
>> Okay.
Well
-
connect the analogy to
the methods one more time.
-
>> Okay.
Sure so for
-
a method you can put in inputs.
-
>> Like we did the quantity for
the display method?
-
>> Yeah, and then you execute
the instructions of the method.
-
>> Okay, almost like the recipe for
making a cake?
-
>> Mm-hm.
-
Then you can have
the output value returned.
-
>> Outputs, they are delicious.
-
>> [LAUGH]
>> Let's look up more information
-
about input parameters for methods.
-
I'm going to do a Google search for
Jjava method parameter.
-
The first link looks good so
I'm going to click on it.
-
Now this article talks about
passing information to a method.
-
Here's a code snippet that computes
the monthly payments for a home loan.
-
It has four input parameters,
loan amount, interest rate, future value
-
of the loan, as well as the number
periods of the length of the loan.
-
And then inside this method we see
that we can use these input parameters
-
simply as variables to do
our math calculations.
-
When we call the compute payment method,
-
we have to make sure we pass in
the inputs in this specific order.
-
So the loan amount has to go first,
and then the rate and so on.
-
For the quiz in this section,
-
you'll have the chance to review
this article in more detail.
-
Within the main activity
of the desk Java app,
-
we're going to be modifying
the display quantity method and
-
the input parameter to
give you some practice.
-
Notice that in the decrement
method of our app,
-
what we call the display
quantity method,
-
we're passing the quantity variable
over as input to this method.
-
But the input parameter
is called 'number', so
-
whenever we want to refer to this input,
we just have to say number.
-
Now technically we could have used
the global quantity variable within this
-
method instead of passing
it as an input parameter.
-
but for the purposes of teaching
you about input parameters,
-
let's just leave it like this.
-
I can change this call to
the display quantity method so
-
it passes input, the number 30.
-
This is valid because it still
matches the input data type.
-
So in the decrement method when
we call it display quantity,
-
it will jump down here and
it will execute these instructions, and
-
the variable number will
have the value of 30.
-
I'll run it on my device to show you,
so now when I run the minus button on my
-
app, it will display
a quantity of 30 here.
-
So let me try that.
-
There.
And it actually happened like that.
-
I can also create a different variable
and pass that as an input to the method.
-
Here I've created a local variable
called some quantity, and
-
I initialized it to 100.
-
I passed this variable
as input to this method,
-
and now the number variable
inside here has a value of 100.
-
Let me run this on the device.
-
Now I expect when I hit the minus
button the quantity will update to 100.
-
And it does.
-
These are just some things
you can do to experiment with
-
passing in different inputs to a method.
-
As you work with input parameters,
-
another term that you might
come across is arguments.
-
When you call a method,
-
the inputs being passed to that
method are called the arguments.
-
Now this is kind of a funny word.
-
Because when you think of arguments,
you think of people yelling and
-
cursing at each other.
-
But this is not like that.
-
Arguments are simply the inputs
being passed to a method.
-
Input parameters are the actual
inputs in your method declaration and
-
those are the variables that
are used within your method.
-
So that's a slight difference between
input parameter and arguments.
-
In any case, go ahead and
read the article and
-
the instructor notes
about method parameters.
-
Then go ahead and
experiment with the code in your app.
-
First try to rename
the number input parameter
-
within the display quantity
method declaration.
-
Just simply give it
a different variable name.
-
Then go ahead and fix any errors,
and then run the app again.
-
For the next test, go ahead and
change the data type
-
of the number input parameter from
an int to something else like String.
-
Observe what happens and
then go ahead and undo the change.
-
If you want to experiment with
other things, go right ahead.
-
For example, if you have a question,
-
what would happen if I do x,
then just go ahead and try it.