As mentioned before, you can return values from a function.
It is bad practice for your function to handle the final result, but it is good practice for your function
to return the final result.
So in this lesson you will create a function that returns a string.
You have been provided with a text file.
Please download it from your resources folder.
Start by copying and pasting the following text underneath your return values class.
Highlight it if you're using a mac press command slash.
If you're using Windows Press control slash to comment out the text and now we can begin.
All right.
We will use the same function signature as before, but this time our function will be returning a string.
So we specify a return type of string and the function name will be explained area.
The function expects to receive a language value when called.
So we define one language parameter of type string.
All right.
You'll notice right away that we get an error.
The error is that the method must return a result of type string.
But right now, our method is not returning anything.
So in order to adhere to the return type, we need to return a string value.
So we will return.
Hello.
And now whenever we call the function, it's going to return a string of hello.
But that's not what we want to do.
What we want is to compare the language that gets passed in.
Against three possible cases.
So the first case is if the language that was passed in is English.
In which case we can return.
Area equals length times width.
If the language that they pass in equals French, then we can return.
The following.
If it's Spanish.
Then we can return the Spanish text.
You'll notice that this time we don't need to put the brake forward.
Why do you think that is?
To put it simply, while brake would only brake, the switch statement return would break the entire
function when invoked.
So you do not need to put brake after returning a value.
It's going to return the corresponding value and immediately break the entire function.
All right.
But now you'll notice we still have an error here that says this method must return a result of type
string.
That is really weird.
If the case is English, we're returning English text.
If the case is French, we're returning French text.
And if the case is Spanish, were returning Spanish text, but the function needs to return a string
no matter what.
Otherwise, it's going to throw an error.
So we did not account for the scenario where a user might say case Italian if they put case Italian.
None of these cases are going to get executed and the function is not going to return anything, but
it needs to return a string no matter what.
So what we have to do is say default, default account for every other scenario that doesn't match these
three.
In that case, we can return.
Language not available.
So whenever you specify a return type, you need to make sure that something gets returned no matter
what gets passed in.
Here we have a return type of double.
This function is always going to return a double no matter what.
Here we have a return type of string.
The function is always going to return a string no matter what the scenario may be.
Now the error is gone, which means we can start playing around.
So here I'm going to say string English explanation.
Is equal to whatever the explain area function returns when we pass in a language of English and we
will copy this for more times.
String French explanation will equal whatever explain area returns when we pass in a language of French
string.
Spanish explanation will equal whatever explain area returns when we pass any language of Spanish and
string.
Italian explanation will equal whatever the explain area function returns when we pass any language
of Italian.
And now what we're going to do is visualize the runtime.
But starting from line number seven.
So put a breakpoints here.
Make sure that you have launched the exact folder that contains your Java file.
Otherwise, visualizing the runtime might not work for you.
And now we can go ahead and press the familiar debug button.
So all of this code ran naturally because we didn't have any breakpoints here.
And you can see that each area variable stores a different value depending on what was passed in in
its respective function call.
And now we move on to line number seven here.
We're calling explain area passing in a value of English.
Once you press, step inside.
Now we can keep pressing step over.
In fact, I recommend you do so.
Otherwise you're going to be taken to the string class and we don't want to deal with that.
In fact, take it as a rule of thumb, But once you already step inside the function, then you can
just keep pressing step over.
So we're going to press step over and the matching case is English return.
What it's going to do is it's going to return this string.
And as soon as it does that, it's immediately going to break the entire function.
The explained area function call retained the value that was returned.
And here we're setting the English explanation variable equal to that return value.
Moving to the next breakpoint here, we're calling the explained area function again, this time passing
in a value of French.
The parameter stores the value that was passed in.
And here we can compare that value against three possible cases.
The case match is case French.
As soon as the return keyword gets invoked, it's going to return the following string.
And as soon as it does return the string return is always the last thing that ever runs any function,
so it's automatically going to break the entire function.
The explained area function call retains, it holds onto the value that was returned, which were then
storing in a string variable French explanation.
All right, now we have two variables, each one equaling a different explanation depending on what
was returned by the function when it was called.
And now we can just press continue.
The Spanish explanation equals the Spanish explanation that was returned by the function.
And Italian explanation is going to equal.
I should have pressed step over instead of continue, because then it just continued to the next breakpoint.
There was no breakpoint and I just terminated the runtime.
So let me re visualize the runtime only putting a breakpoint at line number ten.
I'm going to step over this line instead of continuing execution.
The Italian explanation variable equals whatever was returned by the function when it was called.
We can assume the default case was the one that executed returned a value of language not available.
The function call held onto the returned value, which was then stored in a variable called string Italian
Explanation.
All right, we just created another function, but this one returns a string and this one returns a
double.
But I find this function much more interesting because we've got some conditional logic going on in
here.
Let us apply some conditional logic in here as well.
So in real life, you would never have a negative length or a negative width.
So what we can say is if length.
Is smaller than zero.
Or if the width is smaller than zero, then clearly the values that they passed in are invalid.
So we're just going to say System.out.println in valid dimensions.
And then we can say system.
Dot exit zero.
So if one of the values that gets passed in is negative, then we're going to print out a message telling
whoever called the function that they messed up and we're going to terminate the runtime.
So we will call the function again, this time passing any value of negative five and a width of, you
know what?
We will keep the width valid.
All right, Let me just put a breakpoint over here.
Debugging the runtime.
Stepping inside of calculate area.
So this if statement will only execute if the length is smaller than zero or if the width is smaller
than zero, the length is negative five, which means this is going to evaluate to true.
Because one of the comparisons is true.
This entire condition is going to become true courtesy of the ore operator, which means it's going
to print invalid dimensions.
And it's going to terminate the runtime.
All right.
That is all you created functions that return values.
In order to do a final recap, let us visualize the runtime everywhere.
So first we're calling the function calculate area passing in two values.
The parameter length stores.
The first value that was passed in the parameter width store is the second value that was passed in
here.
We're checking if one of these values is less than zero, but because none of these are zero, both
of these are going to evaluate to false, which means the entire condition is false.
So this will get skipped.
Here.
It calculates an area that equals 8.28.
Here we are returning the area.
The function call holds on to the area value that was returned, which were then storing in a variable
called area one.
Here.
We can just continue to the next breakpoint.
Area two equals 3.84.
Area three equals the value that was produced by the calculate area function when we passed in these
values.
And now here if we step inside.
Unfortunately, the length equals negative five, which means this will evaluate to true meaning.
The entire condition is going to become true courtesy of the operator.
It's therefore going to print invalid dimensions and our application will terminate right here and now.
But if we were to comment this out, we visualize the runtime.
Continue.
Continue, continue.
Each area store is a value that was produced by the calculate area function depending on what was passed
in.
And here we can press continue.
Continue, continue.
Stepping over the final line.
And here every string variable equals a string that was returned by the explained area function depending
on which value was passed in.
All right.
That is all we're going to cover for return values.
I really hope you've been enjoying these breakpoint sessions.