1 00:00:00,180 --> 00:00:04,230 Most functions rely on parameters to perform their task. 2 00:00:06,140 --> 00:00:09,860 Functions with parameters expect to receive a values. 3 00:00:11,070 --> 00:00:12,630 Functions without parameters. 4 00:00:12,630 --> 00:00:15,450 Do not expect to receive any values. 5 00:00:17,080 --> 00:00:20,680 In this lesson, you will create a function that defines parameters. 6 00:00:22,510 --> 00:00:28,390 First create a new file named Parameters Java and make sure the class has a main method. 7 00:00:33,940 --> 00:00:37,990 The area of a rectangle, as you know, is equal to length times width. 8 00:00:39,320 --> 00:00:44,060 You've been given three rectangles and each rectangle has different dimensions. 9 00:00:44,210 --> 00:00:47,600 Our job is to calculate the area of each rectangle. 10 00:00:51,550 --> 00:00:52,780 How will we do this? 11 00:00:52,810 --> 00:00:57,310 Step one is to create a function that expects to receive a length and a width. 12 00:00:57,850 --> 00:01:00,460 Here I have a function called calculate area. 13 00:01:00,490 --> 00:01:05,200 The function expects to receive two double values a length and a width. 14 00:01:06,870 --> 00:01:10,800 A parameter store is a value that your function expects to receive. 15 00:01:12,980 --> 00:01:16,300 The function calculate area defines two parameters. 16 00:01:16,310 --> 00:01:19,490 Therefore it expects to receive two values. 17 00:01:22,030 --> 00:01:26,380 The next step is to call the function by passing in an equal number of values. 18 00:01:28,060 --> 00:01:33,580 In order to call the function calculate area, you need to pass in one value for every parameter that 19 00:01:33,580 --> 00:01:34,600 it defines. 20 00:01:36,700 --> 00:01:40,450 A value that you pass into a function is known as an argument. 21 00:01:42,630 --> 00:01:46,390 The function calculate area defines to double parameters. 22 00:01:46,410 --> 00:01:49,560 Each parameter expects to receive a double value. 23 00:01:49,950 --> 00:01:54,240 So in order to call the function, you need to pass in to double values. 24 00:01:54,240 --> 00:01:57,810 The actual values that we pass in are called arguments. 25 00:02:04,220 --> 00:02:08,479 In step three, the function uses its parameters to perform a task. 26 00:02:09,690 --> 00:02:13,890 The function calculate area uses its parameters to calculate an area. 27 00:02:14,640 --> 00:02:18,660 If we were to trace the runtime first we're calling calculate area. 28 00:02:18,690 --> 00:02:21,780 The values that we pass into the function are arguments. 29 00:02:21,780 --> 00:02:26,910 When the function gets invoked, the first parameter stores the first value that was passed, then the 30 00:02:26,910 --> 00:02:29,940 second parameter stores the second value that was passed in. 31 00:02:30,850 --> 00:02:35,110 The function uses the value inside each parameter to perform its task. 32 00:02:35,140 --> 00:02:37,750 It calculates an area and prints it. 33 00:02:38,110 --> 00:02:40,690 You might be wondering why is this beneficial? 34 00:02:40,870 --> 00:02:41,880 Think about it. 35 00:02:41,890 --> 00:02:46,930 If you want to calculate the area for three different rectangles, I can just call this function three 36 00:02:46,930 --> 00:02:51,100 different times each time passing in a different set of arguments. 37 00:02:51,520 --> 00:02:56,380 If I call the function and pass in these values, the first parameter stores the first value that was 38 00:02:56,380 --> 00:03:01,120 passed, then the second parameter stores, the second value that was passed in the function uses the 39 00:03:01,120 --> 00:03:06,760 value inside each parameter to calculate an area, and it follows that the function can calculate an 40 00:03:06,760 --> 00:03:10,060 area for any pair of values that get passed in. 41 00:03:10,060 --> 00:03:12,490 The function is completely reusable. 42 00:03:18,990 --> 00:03:24,330 Let's do this in code, starting with step one, creating a function that expects to receive a length 43 00:03:24,330 --> 00:03:25,440 and a width. 44 00:03:27,070 --> 00:03:27,550 All right. 45 00:03:27,550 --> 00:03:30,490 Our function is going to be publicly accessible. 46 00:03:31,720 --> 00:03:32,650 It will be void. 47 00:03:32,650 --> 00:03:34,660 It's not going to return any values. 48 00:03:34,660 --> 00:03:37,390 We'll talk about return values in the next video. 49 00:03:37,690 --> 00:03:41,050 And we're going to call the function calculate area. 50 00:03:42,590 --> 00:03:46,280 Our function calculate area expects to receive two values. 51 00:03:46,280 --> 00:03:53,210 So we need to define two parameters double length and double width. 52 00:03:56,000 --> 00:04:01,520 Think of a parameter as a variable that just stores whatever value gets passed into our function. 53 00:04:02,410 --> 00:04:08,110 So a double length will store the first value that gets passed in and double width will store the second 54 00:04:08,110 --> 00:04:09,490 value that gets passed in. 55 00:04:11,280 --> 00:04:15,120 Task two is to call the function by simply passing in two values. 56 00:04:16,500 --> 00:04:18,959 I'm going to call the function calculate area. 57 00:04:19,730 --> 00:04:25,280 And the reason this is not auto completing is you'll remember the function calculate area needs to be 58 00:04:25,280 --> 00:04:29,860 static as well in order for it to be called from the static main function. 59 00:04:29,870 --> 00:04:31,660 Don't worry about what static does. 60 00:04:31,670 --> 00:04:35,660 We will talk more about static in module two object oriented programming. 61 00:04:35,660 --> 00:04:39,500 For now, just know that we need to have it in order to call this function for main. 62 00:04:39,500 --> 00:04:47,060 And now I'm going to calculate the area for a rectangle with a length of 2.3 and a width of 3.6. 63 00:04:47,850 --> 00:04:50,700 The values that I'm passing in are called arguments. 64 00:04:50,700 --> 00:04:55,030 The variables that will end up storing these values are called parameters. 65 00:04:55,050 --> 00:04:57,960 It's really important that you make that distinction. 66 00:04:59,030 --> 00:05:03,650 In step number three, the function is going to use its parameters to perform a task. 67 00:05:03,650 --> 00:05:09,440 So when this function is called, it's going to receive two values where each value gets stored inside 68 00:05:09,440 --> 00:05:10,400 of a parameter. 69 00:05:10,400 --> 00:05:13,910 And now we can use the parameter values to calculate an area. 70 00:05:13,940 --> 00:05:17,450 Double area is equal to length times width. 71 00:05:18,380 --> 00:05:21,800 And after we calculate the area, we can print it. 72 00:05:21,800 --> 00:05:24,800 System, dot, dot, print line. 73 00:05:26,250 --> 00:05:32,370 The area is equal to the area that we calculated and we are done. 74 00:05:33,140 --> 00:05:38,210 And now, instead of running our code the normal way, we will visualize the runtime as we've been doing 75 00:05:38,210 --> 00:05:39,760 in the workbook solutions. 76 00:05:39,770 --> 00:05:44,120 So I'm going to put a breakpoint here and that's it. 77 00:05:44,540 --> 00:05:49,310 And now you'll remember that for this to work, for us to be able to visualize the runtime, you need 78 00:05:49,310 --> 00:05:54,110 to launch the exact folder that contains your Java file, in this case, section four. 79 00:05:54,110 --> 00:05:57,320 And now we can press debug to visualize the runtime. 80 00:05:59,490 --> 00:06:06,000 And in order to step inside the function that I'm calling, I need to press the button, step into. 81 00:06:07,430 --> 00:06:14,210 All right, so I'm calling the function calculate area, passing in two values, passing in two arguments. 82 00:06:14,210 --> 00:06:20,780 The first parameter length stores, the first value that was passed in the second parameter stores, 83 00:06:20,780 --> 00:06:22,580 the second value that gets passed in. 84 00:06:22,580 --> 00:06:27,050 So now length equals 2.3 and width equals 3.6. 85 00:06:27,050 --> 00:06:32,840 And to further prove to you that parameters are essentially just variables, press this button and you 86 00:06:32,840 --> 00:06:36,740 can visualize the length variable and the width of variable. 87 00:06:37,250 --> 00:06:43,880 And now we can use the values inside of each parameter, the values inside of each variable to calculate 88 00:06:43,880 --> 00:06:48,470 an area for a rectangle with a length of 2.3 and a width of 3.6. 89 00:06:51,490 --> 00:06:55,390 And the area for such a rectangle is 8.28. 90 00:06:55,870 --> 00:07:04,000 Once you reach this line, do not press, step in to press, continue to resume execution naturally. 91 00:07:04,180 --> 00:07:09,430 And the area for a rectangle with such dimensions is 8.28. 92 00:07:10,200 --> 00:07:14,160 What's beautiful about this is that the function is reusable. 93 00:07:14,190 --> 00:07:20,290 I can call it as many times as I want to calculate the area for any rectangle that I want. 94 00:07:20,310 --> 00:07:28,860 I can calculate the area for a rectangle with a length of 1.6 and a width of 2.4 for a length of 2.6 95 00:07:28,860 --> 00:07:32,340 and a width of 4.2. 96 00:07:32,370 --> 00:07:33,630 It doesn't matter. 97 00:07:33,630 --> 00:07:40,260 I can reuse the function as many times as I want to, and this is a really, really powerful way of 98 00:07:40,260 --> 00:07:40,950 coding. 99 00:07:41,250 --> 00:07:44,370 All right, let us visualize the runtime once more. 100 00:07:45,570 --> 00:07:49,160 So here I'm calling the function calculate area and line number three. 101 00:07:49,170 --> 00:07:54,000 I'm going to step inside the function that we're calling the length parameter stores. 102 00:07:54,000 --> 00:07:59,220 The first value that was passed in the width parameter stores, the second value that was passed in. 103 00:07:59,670 --> 00:08:01,800 I will keep stepping into the function. 104 00:08:01,800 --> 00:08:07,920 The area based on the values that were passed in is equal to 8.28 and now do not make the mistake of 105 00:08:07,920 --> 00:08:13,350 pressing step into because then it's going to step into the print line function, which we don't want 106 00:08:13,350 --> 00:08:17,130 to do press step over to run this line. 107 00:08:17,130 --> 00:08:19,590 And the area is 8.28. 108 00:08:19,590 --> 00:08:24,930 And now if I want to continue to the next the breakpoint, I can press the continue button. 109 00:08:26,490 --> 00:08:27,510 All right. 110 00:08:27,540 --> 00:08:34,710 Now I'm calling the function again, passing in a value of 1.6 and another argument that equals 2.4, 111 00:08:34,710 --> 00:08:40,110 stepping into the function that we're calling the first parameter stores, the first value that was 112 00:08:40,110 --> 00:08:44,460 passed in the second parameter store is the second value that was passed in. 113 00:08:44,460 --> 00:08:49,080 And now we're calculating an area based on the parameter values. 114 00:08:51,070 --> 00:08:55,300 If I keep stepping into area equals 3.84. 115 00:08:55,330 --> 00:09:00,010 Don't make the mistake of pressing step in because then it's going to step into the print line function, 116 00:09:00,010 --> 00:09:01,360 which we don't want to do. 117 00:09:02,160 --> 00:09:09,180 We will step over this line and it prints area is equal to 3.8 for continuing to the next breakpoint. 118 00:09:09,180 --> 00:09:15,210 Stepping into the function again, the length parameter equals 2.6, the width parameter equals 4.2. 119 00:09:15,240 --> 00:09:21,120 We calculate an area based on these parameter values and then we can print the area. 120 00:09:22,180 --> 00:09:23,560 10.92. 121 00:09:23,560 --> 00:09:28,930 And now if I press continue because there are no other break points that come after the run, time will 122 00:09:28,930 --> 00:09:30,310 just terminate. 123 00:09:30,760 --> 00:09:31,630 Beautiful. 124 00:09:33,840 --> 00:09:38,880 So to recap, a parameter store is a value that your function expects to receive. 125 00:09:40,840 --> 00:09:46,750 When a function defines parameters, then in order to call it, you need to pass in a matching number 126 00:09:46,750 --> 00:09:50,590 of arguments based on the position of the parameter. 127 00:09:50,590 --> 00:09:55,870 It's going to store the value that gets passed in, and then we can use these values to perform our 128 00:09:55,870 --> 00:09:56,620 task.