Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
You can return values from a function.
It's bad practice for your function to handle the final results.
But it's good practice for your function to return the final results.
Your function is going to return the area.
Start by creating a new file called Return Values Dot Java and make sure that the class has a main method.
First thing we'll do is just copy all the code from earlier and to return values.
Java.
We currently have a problem in that.
Our function here is performing two tasks.
It performs the task of calculating an area, and it also performs another task of printing that same
area.
At this point we might as well call the function, calculate area and print it.
Obviously, we're not going to do that.
It is bad practice for your function to handle the final result.
It should return the final result.
So in step one, your function must define a return type.
The return type specifies what type of value that your function will return.
We expect to return a double, so the function's return type is going to be double.
In step two, your function needs to return a value.
The function calculate area returns the final result the area that it calculates.
And the value that you return must always match the return type.
The return type is double, so the value that the function returns needs to be a double.
The function caller retains it holds on to the return value.
First we're calling calculate area.
When the function gets invoked, the first parameter stores the first value that was passed in.
The second parameter stores.
The second value that was passed in.
The function uses the value inside of each parameter to perform its task.
It calculates an area and then it returns it.
The function call retains, it holds on to the value that was returned.
And we can store the return value in a variable.
If you want to store the area for three different rectangles, I can just call this function three 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 in the second parameter stores, the second value that was passed in the function uses the value
inside of each parameter to calculate an area.
The function returns the final result.
The function call holds on to the return value, which we can store inside of a variable.
Every time I call the function, I can store the final result that it produces inside of a variable.
Let's do this in code, starting with step one, in that your function must define a return type.
The return type specifies what type of value this function will return.
We expect to return a double, so the function's return type will be double.
Step two is for your function to return a value.
Your function should not be the one to handle the final result.
So we're going to delete this print statement and instead what this function will do is return the final
result, the area that it calculates.
And the value that you return must always match the return type.
The return type is a double, so the value that the function returns needs to be a double as well.
And our area variable is a double.
And step number three, the function caller retains it holds on to the return value.
And the only way to confirm this is by visualizing the runtime.
So let's put three breakpoints over here and press the debug button.
Let's step inside of calculate area.
The length that was passed in is 2.3.
The width that was passed in is 3.6.
As a result, we produce an area that equals 8.28.
If I step over.
And here we can see that our function call holds on to the value 8.28, and then we can store the double
that it retains inside of a variable.
So if we continue to the next breakpoint, now we're calling calculate area again, this time passing
in arguments of 1.6 and 2.4.
Each parameter store is a value that was passed in so length stores 1.6 width stores 2.4.
And now we're using these values to calculate an area and here we're returning the area that the function
produced.
Upon doing so, this function call holds on to the return value of 3.84, which we can then store in
a double variable.
So let's do that right now.
I'm going to stop the runtime.
As we hover over calculate area, it tells you that it returns a double, which we can then store in
a double variable.
Double area one.
Do the same thing throughout.
All right.
Re visualizing the runtime.
Here we're calling calculate area passing in values of 2.3 and 3.6.
Stepping inside of the function, we calculate an area based on the values that were passed in stepping
inside even more.
Here we're returning the area that was produced by our calculate area function.
Stepping over the function call, calculate area holds on to the return value of 8.28, and then we're
storing that return value inside of double area one.
Continuing to the next breakpoint.
Now area one equals 8.28 and every time we call this function, the final result that the function produces
gets stored in a variable.
So area two equals 3.84 and area three is going to equal 10.92.
And now by pressing continue, since there are no other breakpoints, the runtime finishes.
So you learned how to return a value from a function.
When you call a function, it's going to use the value inside of each parameter to calculate an area.
The function returns the final result.
The function call holds on to the return value, which we can then store inside of a variable.
Every time I call the function, I can store the final result in a variable.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.