Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,240 --> 00:00:03,270
As mentioned before, you can return values from a function.
2
00:00:05,040 --> 00:00:10,440
It is bad practice for your function to handle the final result, but it is good practice for your function
3
00:00:10,440 --> 00:00:12,310
to return the final result.
4
00:00:12,330 --> 00:00:16,260
So in this lesson you will create a function that returns a string.
5
00:00:17,580 --> 00:00:19,660
You have been provided with a text file.
6
00:00:19,680 --> 00:00:22,140
Please download it from your resources folder.
7
00:00:27,670 --> 00:00:33,760
Start by copying and pasting the following text underneath your return values class.
8
00:00:34,240 --> 00:00:37,290
Highlight it if you're using a mac press command slash.
9
00:00:37,300 --> 00:00:42,550
If you're using Windows Press control slash to comment out the text and now we can begin.
10
00:00:43,120 --> 00:00:43,660
All right.
11
00:00:43,660 --> 00:00:50,320
We will use the same function signature as before, but this time our function will be returning a string.
12
00:00:50,320 --> 00:00:56,620
So we specify a return type of string and the function name will be explained area.
13
00:00:57,590 --> 00:01:01,160
The function expects to receive a language value when called.
14
00:01:01,160 --> 00:01:05,090
So we define one language parameter of type string.
15
00:01:06,190 --> 00:01:07,060
All right.
16
00:01:07,510 --> 00:01:09,640
You'll notice right away that we get an error.
17
00:01:09,670 --> 00:01:14,160
The error is that the method must return a result of type string.
18
00:01:14,170 --> 00:01:17,010
But right now, our method is not returning anything.
19
00:01:17,020 --> 00:01:22,420
So in order to adhere to the return type, we need to return a string value.
20
00:01:22,450 --> 00:01:23,710
So we will return.
21
00:01:23,710 --> 00:01:24,340
Hello.
22
00:01:27,020 --> 00:01:31,010
And now whenever we call the function, it's going to return a string of hello.
23
00:01:31,020 --> 00:01:32,790
But that's not what we want to do.
24
00:01:32,820 --> 00:01:37,830
What we want is to compare the language that gets passed in.
25
00:01:39,120 --> 00:01:41,130
Against three possible cases.
26
00:01:41,580 --> 00:01:47,040
So the first case is if the language that was passed in is English.
27
00:01:48,290 --> 00:01:50,240
In which case we can return.
28
00:01:51,150 --> 00:01:53,670
Area equals length times width.
29
00:01:56,610 --> 00:02:01,830
If the language that they pass in equals French, then we can return.
30
00:02:02,580 --> 00:02:03,630
The following.
31
00:02:07,080 --> 00:02:08,340
If it's Spanish.
32
00:02:09,180 --> 00:02:11,550
Then we can return the Spanish text.
33
00:02:16,210 --> 00:02:19,930
You'll notice that this time we don't need to put the brake forward.
34
00:02:19,960 --> 00:02:21,550
Why do you think that is?
35
00:02:22,120 --> 00:02:28,390
To put it simply, while brake would only brake, the switch statement return would break the entire
36
00:02:28,390 --> 00:02:29,770
function when invoked.
37
00:02:29,770 --> 00:02:33,520
So you do not need to put brake after returning a value.
38
00:02:34,610 --> 00:02:40,070
It's going to return the corresponding value and immediately break the entire function.
39
00:02:40,490 --> 00:02:41,360
All right.
40
00:02:41,360 --> 00:02:46,820
But now you'll notice we still have an error here that says this method must return a result of type
41
00:02:46,820 --> 00:02:47,420
string.
42
00:02:47,450 --> 00:02:48,780
That is really weird.
43
00:02:48,800 --> 00:02:51,790
If the case is English, we're returning English text.
44
00:02:51,800 --> 00:02:54,560
If the case is French, we're returning French text.
45
00:02:54,560 --> 00:03:00,890
And if the case is Spanish, were returning Spanish text, but the function needs to return a string
46
00:03:00,890 --> 00:03:02,360
no matter what.
47
00:03:02,390 --> 00:03:04,370
Otherwise, it's going to throw an error.
48
00:03:04,520 --> 00:03:12,990
So we did not account for the scenario where a user might say case Italian if they put case Italian.
49
00:03:13,010 --> 00:03:19,070
None of these cases are going to get executed and the function is not going to return anything, but
50
00:03:19,070 --> 00:03:22,040
it needs to return a string no matter what.
51
00:03:22,640 --> 00:03:29,630
So what we have to do is say default, default account for every other scenario that doesn't match these
52
00:03:29,630 --> 00:03:30,240
three.
53
00:03:30,260 --> 00:03:32,570
In that case, we can return.
54
00:03:33,720 --> 00:03:37,110
Language not available.
55
00:03:40,290 --> 00:03:46,890
So whenever you specify a return type, you need to make sure that something gets returned no matter
56
00:03:46,890 --> 00:03:48,400
what gets passed in.
57
00:03:48,420 --> 00:03:50,430
Here we have a return type of double.
58
00:03:50,460 --> 00:03:53,760
This function is always going to return a double no matter what.
59
00:03:53,790 --> 00:03:55,650
Here we have a return type of string.
60
00:03:55,680 --> 00:04:00,480
The function is always going to return a string no matter what the scenario may be.
61
00:04:00,510 --> 00:04:03,830
Now the error is gone, which means we can start playing around.
62
00:04:03,840 --> 00:04:07,860
So here I'm going to say string English explanation.
63
00:04:08,790 --> 00:04:16,140
Is equal to whatever the explain area function returns when we pass in a language of English and we
64
00:04:16,140 --> 00:04:18,959
will copy this for more times.
65
00:04:19,079 --> 00:04:26,610
String French explanation will equal whatever explain area returns when we pass in a language of French
66
00:04:26,970 --> 00:04:27,660
string.
67
00:04:27,660 --> 00:04:34,650
Spanish explanation will equal whatever explain area returns when we pass any language of Spanish and
68
00:04:34,650 --> 00:04:35,130
string.
69
00:04:35,130 --> 00:04:41,400
Italian explanation will equal whatever the explain area function returns when we pass any language
70
00:04:41,400 --> 00:04:42,510
of Italian.
71
00:04:43,080 --> 00:04:46,440
And now what we're going to do is visualize the runtime.
72
00:04:46,440 --> 00:04:48,450
But starting from line number seven.
73
00:04:48,450 --> 00:04:50,250
So put a breakpoints here.
74
00:04:50,250 --> 00:04:54,570
Make sure that you have launched the exact folder that contains your Java file.
75
00:04:54,600 --> 00:04:57,780
Otherwise, visualizing the runtime might not work for you.
76
00:04:57,780 --> 00:05:01,620
And now we can go ahead and press the familiar debug button.
77
00:05:01,920 --> 00:05:06,480
So all of this code ran naturally because we didn't have any breakpoints here.
78
00:05:06,480 --> 00:05:11,550
And you can see that each area variable stores a different value depending on what was passed in in
79
00:05:11,550 --> 00:05:13,230
its respective function call.
80
00:05:13,230 --> 00:05:17,370
And now we move on to line number seven here.
81
00:05:17,370 --> 00:05:20,520
We're calling explain area passing in a value of English.
82
00:05:20,520 --> 00:05:23,040
Once you press, step inside.
83
00:05:23,040 --> 00:05:25,560
Now we can keep pressing step over.
84
00:05:25,560 --> 00:05:27,600
In fact, I recommend you do so.
85
00:05:27,630 --> 00:05:32,190
Otherwise you're going to be taken to the string class and we don't want to deal with that.
86
00:05:32,190 --> 00:05:36,270
In fact, take it as a rule of thumb, But once you already step inside the function, then you can
87
00:05:36,270 --> 00:05:37,950
just keep pressing step over.
88
00:05:37,950 --> 00:05:42,540
So we're going to press step over and the matching case is English return.
89
00:05:42,540 --> 00:05:45,510
What it's going to do is it's going to return this string.
90
00:05:45,510 --> 00:05:50,130
And as soon as it does that, it's immediately going to break the entire function.
91
00:05:52,810 --> 00:05:57,160
The explained area function call retained the value that was returned.
92
00:05:57,160 --> 00:06:01,750
And here we're setting the English explanation variable equal to that return value.
93
00:06:03,020 --> 00:06:09,260
Moving to the next breakpoint here, we're calling the explained area function again, this time passing
94
00:06:09,260 --> 00:06:10,650
in a value of French.
95
00:06:10,670 --> 00:06:13,160
The parameter stores the value that was passed in.
96
00:06:13,160 --> 00:06:17,080
And here we can compare that value against three possible cases.
97
00:06:17,090 --> 00:06:19,760
The case match is case French.
98
00:06:21,870 --> 00:06:26,250
As soon as the return keyword gets invoked, it's going to return the following string.
99
00:06:26,250 --> 00:06:32,220
And as soon as it does return the string return is always the last thing that ever runs any function,
100
00:06:32,220 --> 00:06:35,420
so it's automatically going to break the entire function.
101
00:06:35,430 --> 00:06:41,490
The explained area function call retains, it holds onto the value that was returned, which were then
102
00:06:41,490 --> 00:06:45,150
storing in a string variable French explanation.
103
00:06:45,150 --> 00:06:51,480
All right, now we have two variables, each one equaling a different explanation depending on what
104
00:06:51,480 --> 00:06:54,300
was returned by the function when it was called.
105
00:06:54,300 --> 00:06:56,730
And now we can just press continue.
106
00:06:56,880 --> 00:07:02,220
The Spanish explanation equals the Spanish explanation that was returned by the function.
107
00:07:04,040 --> 00:07:06,890
And Italian explanation is going to equal.
108
00:07:07,850 --> 00:07:13,630
I should have pressed step over instead of continue, because then it just continued to the next breakpoint.
109
00:07:13,640 --> 00:07:16,610
There was no breakpoint and I just terminated the runtime.
110
00:07:16,610 --> 00:07:22,280
So let me re visualize the runtime only putting a breakpoint at line number ten.
111
00:07:22,370 --> 00:07:26,060
I'm going to step over this line instead of continuing execution.
112
00:07:26,800 --> 00:07:32,480
The Italian explanation variable equals whatever was returned by the function when it was called.
113
00:07:32,500 --> 00:07:38,540
We can assume the default case was the one that executed returned a value of language not available.
114
00:07:38,560 --> 00:07:44,800
The function call held onto the returned value, which was then stored in a variable called string Italian
115
00:07:44,800 --> 00:07:45,670
Explanation.
116
00:07:45,910 --> 00:07:52,750
All right, we just created another function, but this one returns a string and this one returns a
117
00:07:52,750 --> 00:07:53,250
double.
118
00:07:53,260 --> 00:07:58,600
But I find this function much more interesting because we've got some conditional logic going on in
119
00:07:58,600 --> 00:07:58,990
here.
120
00:07:59,020 --> 00:08:02,340
Let us apply some conditional logic in here as well.
121
00:08:02,350 --> 00:08:08,600
So in real life, you would never have a negative length or a negative width.
122
00:08:08,620 --> 00:08:12,220
So what we can say is if length.
123
00:08:13,110 --> 00:08:14,670
Is smaller than zero.
124
00:08:15,920 --> 00:08:22,570
Or if the width is smaller than zero, then clearly the values that they passed in are invalid.
125
00:08:22,580 --> 00:08:29,420
So we're just going to say System.out.println in valid dimensions.
126
00:08:30,320 --> 00:08:32,270
And then we can say system.
127
00:08:33,169 --> 00:08:34,940
Dot exit zero.
128
00:08:36,400 --> 00:08:42,580
So if one of the values that gets passed in is negative, then we're going to print out a message telling
129
00:08:42,580 --> 00:08:46,600
whoever called the function that they messed up and we're going to terminate the runtime.
130
00:08:48,180 --> 00:08:56,190
So we will call the function again, this time passing any value of negative five and a width of, you
131
00:08:56,190 --> 00:08:56,490
know what?
132
00:08:56,490 --> 00:08:58,710
We will keep the width valid.
133
00:09:01,730 --> 00:09:05,030
All right, Let me just put a breakpoint over here.
134
00:09:05,780 --> 00:09:07,100
Debugging the runtime.
135
00:09:08,230 --> 00:09:10,540
Stepping inside of calculate area.
136
00:09:10,540 --> 00:09:16,900
So this if statement will only execute if the length is smaller than zero or if the width is smaller
137
00:09:16,900 --> 00:09:21,760
than zero, the length is negative five, which means this is going to evaluate to true.
138
00:09:21,790 --> 00:09:24,430
Because one of the comparisons is true.
139
00:09:24,460 --> 00:09:29,830
This entire condition is going to become true courtesy of the ore operator, which means it's going
140
00:09:29,830 --> 00:09:32,080
to print invalid dimensions.
141
00:09:32,870 --> 00:09:34,880
And it's going to terminate the runtime.
142
00:09:35,750 --> 00:09:36,310
All right.
143
00:09:36,320 --> 00:09:40,010
That is all you created functions that return values.
144
00:09:40,010 --> 00:09:44,270
In order to do a final recap, let us visualize the runtime everywhere.
145
00:09:48,910 --> 00:09:53,470
So first we're calling the function calculate area passing in two values.
146
00:09:55,210 --> 00:09:57,190
The parameter length stores.
147
00:09:57,190 --> 00:10:02,410
The first value that was passed in the parameter width store is the second value that was passed in
148
00:10:02,620 --> 00:10:02,970
here.
149
00:10:02,980 --> 00:10:08,770
We're checking if one of these values is less than zero, but because none of these are zero, both
150
00:10:08,770 --> 00:10:12,520
of these are going to evaluate to false, which means the entire condition is false.
151
00:10:12,520 --> 00:10:14,050
So this will get skipped.
152
00:10:15,340 --> 00:10:15,670
Here.
153
00:10:15,670 --> 00:10:19,990
It calculates an area that equals 8.28.
154
00:10:20,020 --> 00:10:22,410
Here we are returning the area.
155
00:10:22,420 --> 00:10:27,910
The function call holds on to the area value that was returned, which were then storing in a variable
156
00:10:27,910 --> 00:10:29,350
called area one.
157
00:10:30,370 --> 00:10:30,610
Here.
158
00:10:30,610 --> 00:10:32,500
We can just continue to the next breakpoint.
159
00:10:32,500 --> 00:10:35,020
Area two equals 3.84.
160
00:10:35,140 --> 00:10:40,540
Area three equals the value that was produced by the calculate area function when we passed in these
161
00:10:40,540 --> 00:10:41,320
values.
162
00:10:41,320 --> 00:10:43,570
And now here if we step inside.
163
00:10:44,790 --> 00:10:50,190
Unfortunately, the length equals negative five, which means this will evaluate to true meaning.
164
00:10:50,190 --> 00:10:54,220
The entire condition is going to become true courtesy of the operator.
165
00:10:54,240 --> 00:11:00,750
It's therefore going to print invalid dimensions and our application will terminate right here and now.
166
00:11:02,150 --> 00:11:06,530
But if we were to comment this out, we visualize the runtime.
167
00:11:07,220 --> 00:11:07,880
Continue.
168
00:11:07,880 --> 00:11:09,230
Continue, continue.
169
00:11:09,350 --> 00:11:14,630
Each area store is a value that was produced by the calculate area function depending on what was passed
170
00:11:14,630 --> 00:11:15,020
in.
171
00:11:16,700 --> 00:11:18,230
And here we can press continue.
172
00:11:18,230 --> 00:11:19,400
Continue, continue.
173
00:11:20,000 --> 00:11:21,890
Stepping over the final line.
174
00:11:22,190 --> 00:11:28,940
And here every string variable equals a string that was returned by the explained area function depending
175
00:11:28,940 --> 00:11:30,860
on which value was passed in.
176
00:11:31,220 --> 00:11:31,880
All right.
177
00:11:31,880 --> 00:11:34,680
That is all we're going to cover for return values.
178
00:11:34,700 --> 00:11:37,760
I really hope you've been enjoying these breakpoint sessions.
16747
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.