Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,660 --> 00:00:02,969
You can return values from a function.
2
00:00:04,960 --> 00:00:08,740
It's bad practice for your function to handle the final results.
3
00:00:10,720 --> 00:00:14,650
But it's good practice for your function to return the final results.
4
00:00:17,390 --> 00:00:19,910
Your function is going to return the area.
5
00:00:22,650 --> 00:00:28,740
Start by creating a new file called Return Values Dot Java and make sure that the class has a main method.
6
00:00:34,180 --> 00:00:39,190
First thing we'll do is just copy all the code from earlier and to return values.
7
00:00:39,400 --> 00:00:40,030
Java.
8
00:00:41,790 --> 00:00:43,800
We currently have a problem in that.
9
00:00:43,800 --> 00:00:46,800
Our function here is performing two tasks.
10
00:00:47,070 --> 00:00:54,240
It performs the task of calculating an area, and it also performs another task of printing that same
11
00:00:54,240 --> 00:00:54,870
area.
12
00:00:55,320 --> 00:01:00,810
At this point we might as well call the function, calculate area and print it.
13
00:01:02,730 --> 00:01:04,830
Obviously, we're not going to do that.
14
00:01:08,900 --> 00:01:12,650
It is bad practice for your function to handle the final result.
15
00:01:14,010 --> 00:01:16,320
It should return the final result.
16
00:01:18,910 --> 00:01:22,660
So in step one, your function must define a return type.
17
00:01:24,050 --> 00:01:28,340
The return type specifies what type of value that your function will return.
18
00:01:28,340 --> 00:01:32,990
We expect to return a double, so the function's return type is going to be double.
19
00:01:35,400 --> 00:01:38,340
In step two, your function needs to return a value.
20
00:01:40,550 --> 00:01:45,320
The function calculate area returns the final result the area that it calculates.
21
00:01:46,330 --> 00:01:49,960
And the value that you return must always match the return type.
22
00:01:49,990 --> 00:01:55,030
The return type is double, so the value that the function returns needs to be a double.
23
00:01:56,940 --> 00:02:00,630
The function caller retains it holds on to the return value.
24
00:02:03,480 --> 00:02:05,640
First we're calling calculate area.
25
00:02:07,060 --> 00:02:11,800
When the function gets invoked, the first parameter stores the first value that was passed in.
26
00:02:11,830 --> 00:02:13,510
The second parameter stores.
27
00:02:13,510 --> 00:02:15,250
The second value that was passed in.
28
00:02:16,470 --> 00:02:20,760
The function uses the value inside of each parameter to perform its task.
29
00:02:20,790 --> 00:02:23,730
It calculates an area and then it returns it.
30
00:02:28,300 --> 00:02:32,470
The function call retains, it holds on to the value that was returned.
31
00:02:33,580 --> 00:02:36,820
And we can store the return value in a variable.
32
00:02:39,790 --> 00:02:45,490
If you want to store the area for three different rectangles, I can just call this function three times
33
00:02:45,490 --> 00:02:48,630
each time passing in a different set of arguments.
34
00:02:48,640 --> 00:02:53,440
If I call the function and pass in these values, the first parameter stores the first value that was
35
00:02:53,440 --> 00:02:59,050
passed in the second parameter stores, the second value that was passed in the function uses the value
36
00:02:59,050 --> 00:03:01,660
inside of each parameter to calculate an area.
37
00:03:01,840 --> 00:03:04,000
The function returns the final result.
38
00:03:05,350 --> 00:03:10,600
The function call holds on to the return value, which we can store inside of a variable.
39
00:03:12,010 --> 00:03:17,650
Every time I call the function, I can store the final result that it produces inside of a variable.
40
00:03:23,390 --> 00:03:29,150
Let's do this in code, starting with step one, in that your function must define a return type.
41
00:03:32,460 --> 00:03:36,770
The return type specifies what type of value this function will return.
42
00:03:36,780 --> 00:03:41,520
We expect to return a double, so the function's return type will be double.
43
00:03:42,320 --> 00:03:45,020
Step two is for your function to return a value.
44
00:03:46,400 --> 00:03:49,880
Your function should not be the one to handle the final result.
45
00:03:49,880 --> 00:03:55,880
So we're going to delete this print statement and instead what this function will do is return the final
46
00:03:55,880 --> 00:03:58,160
result, the area that it calculates.
47
00:04:00,250 --> 00:04:04,680
And the value that you return must always match the return type.
48
00:04:04,690 --> 00:04:11,050
The return type is a double, so the value that the function returns needs to be a double as well.
49
00:04:11,050 --> 00:04:13,570
And our area variable is a double.
50
00:04:14,170 --> 00:04:18,820
And step number three, the function caller retains it holds on to the return value.
51
00:04:20,089 --> 00:04:23,560
And the only way to confirm this is by visualizing the runtime.
52
00:04:23,570 --> 00:04:28,670
So let's put three breakpoints over here and press the debug button.
53
00:04:32,910 --> 00:04:35,070
Let's step inside of calculate area.
54
00:04:35,340 --> 00:04:37,560
The length that was passed in is 2.3.
55
00:04:37,560 --> 00:04:40,050
The width that was passed in is 3.6.
56
00:04:40,140 --> 00:04:45,690
As a result, we produce an area that equals 8.28.
57
00:04:47,500 --> 00:04:48,790
If I step over.
58
00:04:51,470 --> 00:04:59,240
And here we can see that our function call holds on to the value 8.28, and then we can store the double
59
00:04:59,240 --> 00:05:01,550
that it retains inside of a variable.
60
00:05:02,060 --> 00:05:08,420
So if we continue to the next breakpoint, now we're calling calculate area again, this time passing
61
00:05:08,420 --> 00:05:11,310
in arguments of 1.6 and 2.4.
62
00:05:11,900 --> 00:05:18,740
Each parameter store is a value that was passed in so length stores 1.6 width stores 2.4.
63
00:05:18,740 --> 00:05:25,970
And now we're using these values to calculate an area and here we're returning the area that the function
64
00:05:25,970 --> 00:05:26,870
produced.
65
00:05:28,530 --> 00:05:36,510
Upon doing so, this function call holds on to the return value of 3.84, which we can then store in
66
00:05:36,510 --> 00:05:37,770
a double variable.
67
00:05:37,770 --> 00:05:39,780
So let's do that right now.
68
00:05:40,760 --> 00:05:42,380
I'm going to stop the runtime.
69
00:05:42,830 --> 00:05:49,160
As we hover over calculate area, it tells you that it returns a double, which we can then store in
70
00:05:49,160 --> 00:05:50,330
a double variable.
71
00:05:50,390 --> 00:05:51,830
Double area one.
72
00:05:53,350 --> 00:05:55,180
Do the same thing throughout.
73
00:06:03,540 --> 00:06:04,150
All right.
74
00:06:04,180 --> 00:06:05,940
Re visualizing the runtime.
75
00:06:07,270 --> 00:06:12,760
Here we're calling calculate area passing in values of 2.3 and 3.6.
76
00:06:12,760 --> 00:06:18,640
Stepping inside of the function, we calculate an area based on the values that were passed in stepping
77
00:06:18,640 --> 00:06:19,750
inside even more.
78
00:06:19,780 --> 00:06:24,820
Here we're returning the area that was produced by our calculate area function.
79
00:06:25,270 --> 00:06:33,040
Stepping over the function call, calculate area holds on to the return value of 8.28, and then we're
80
00:06:33,040 --> 00:06:36,970
storing that return value inside of double area one.
81
00:06:37,970 --> 00:06:39,890
Continuing to the next breakpoint.
82
00:06:39,920 --> 00:06:47,720
Now area one equals 8.28 and every time we call this function, the final result that the function produces
83
00:06:47,720 --> 00:06:49,520
gets stored in a variable.
84
00:06:49,520 --> 00:06:56,690
So area two equals 3.84 and area three is going to equal 10.92.
85
00:06:56,690 --> 00:07:02,000
And now by pressing continue, since there are no other breakpoints, the runtime finishes.
86
00:07:04,690 --> 00:07:07,750
So you learned how to return a value from a function.
87
00:07:09,880 --> 00:07:14,890
When you call a function, it's going to use the value inside of each parameter to calculate an area.
88
00:07:16,560 --> 00:07:18,900
The function returns the final result.
89
00:07:18,930 --> 00:07:24,690
The function call holds on to the return value, which we can then store inside of a variable.
90
00:07:25,410 --> 00:07:29,490
Every time I call the function, I can store the final result in a variable.
8924
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.