1 00:00:00,840 --> 00:00:03,760 We've managed to write a function that simplifies our code but 2 00:00:03,760 --> 00:00:04,910 there are certain limitations. 3 00:00:06,060 --> 00:00:09,230 When we call the function, we always get the exact same value back. 4 00:00:10,420 --> 00:00:12,890 Since our app will be providing area measurements for 5 00:00:12,890 --> 00:00:15,130 different rooms, this isn't going to work at all. 6 00:00:16,320 --> 00:00:19,910 We could simply change the values of length and width in the function, but 7 00:00:19,910 --> 00:00:21,030 that's not what we want to do. 8 00:00:22,150 --> 00:00:26,550 Going back to our coffee maker analogy, imagine if every time we wanted to make 9 00:00:26,550 --> 00:00:31,270 coffee, and the amount of water in the machine was fixed at one cup. 10 00:00:31,270 --> 00:00:33,310 We could never make more than a single cup of coffee. 11 00:00:34,350 --> 00:00:38,490 Luckily, this isn't how functions, or our coffee makers, actually work. 12 00:00:39,580 --> 00:00:43,370 A coffee maker takes two inputs, coffee and water. 13 00:00:43,370 --> 00:00:46,590 And then uses those inputs to make coffee. 14 00:00:46,590 --> 00:00:50,270 Just like the coffee maker, functions in Swift can take inputs as well. 15 00:00:51,400 --> 00:00:53,720 Let's go back to the function we wrote in the last video. 16 00:00:55,610 --> 00:00:57,550 In the function declaration for 17 00:00:57,550 --> 00:01:01,780 the function area, that is where the keyword func and 18 00:01:01,780 --> 00:01:07,560 the name of the function are specified, we included an empty set of parentheses. 19 00:01:07,560 --> 00:01:10,840 You may have been wondering what purpose that served when we wrote that, 20 00:01:10,840 --> 00:01:14,000 because it doesn't seem to be doing anything right now. 21 00:01:14,000 --> 00:01:20,600 The parentheses following a function name is used to provide a function with inputs. 22 00:01:20,600 --> 00:01:23,630 Let's add an input for a length to our function. 23 00:01:23,630 --> 00:01:27,390 So inside these parentheses, write out the following line of code. 24 00:01:27,390 --> 00:01:34,887 First we'll say (length: Int). 25 00:01:34,887 --> 00:01:38,510 Now this should raise an error, but that's okay, we'll fix it. 26 00:01:38,510 --> 00:01:41,990 What we just did is add an input to our function. 27 00:01:41,990 --> 00:01:47,300 This input that you see right here is called a parameter. 28 00:01:47,300 --> 00:01:53,229 We gave the parameter a name, length, and we said that it should be of type Int. 29 00:01:53,229 --> 00:01:57,348 Now again the syntax to do that is first to give it a name, then a colon, and 30 00:01:57,348 --> 00:02:01,570 then you specify the type of the value that you're expecting. 31 00:02:01,570 --> 00:02:03,950 Now inside the body of the function, 32 00:02:03,950 --> 00:02:07,690 we can get rid of this line of code the hard coded value of length. 33 00:02:08,950 --> 00:02:13,750 What we're saying now, instead of using the value of length specified 34 00:02:13,750 --> 00:02:17,530 inside the body of the function, like we had earlier, 35 00:02:17,530 --> 00:02:22,720 we're going to use the value that we pass into the function through the parameter. 36 00:02:22,720 --> 00:02:25,480 What does this mean exactly, passed through? 37 00:02:25,480 --> 00:02:26,560 We'll get there. 38 00:02:26,560 --> 00:02:30,810 Now just like the constant we created inside the body of the function, 39 00:02:30,810 --> 00:02:35,770 we can use this link parameter in the calculation of the area. 40 00:02:35,770 --> 00:02:40,500 This length that you see here is the same as this length. 41 00:02:40,500 --> 00:02:43,030 Now, this might not make sense yet. 42 00:02:43,030 --> 00:02:46,730 We're calculating an area inside the body of the function, but 43 00:02:46,730 --> 00:02:50,220 there isn't any actual value that we can see. 44 00:02:50,220 --> 00:02:53,550 So, let's get rid of these lines of code where we call the function and 45 00:02:53,550 --> 00:02:54,930 type it out again. 46 00:02:54,930 --> 00:02:59,880 So, if you start typing area, you'll notice that Xcode autocompletes, and 47 00:02:59,880 --> 00:03:02,310 this time, the function looks different. 48 00:03:02,310 --> 00:03:06,860 It specifies the length parameter, and if you hit Enter, you'll notice that it has 49 00:03:06,860 --> 00:03:11,030 a placeholder because it expects us to provide a value for it. 50 00:03:11,030 --> 00:03:12,500 Let's write the value 10 now. 51 00:03:13,550 --> 00:03:17,140 Immediately, you'll see that the value printed in the results area is 120, 52 00:03:17,140 --> 00:03:22,850 because in the calculation of area we're using this value 10 now 53 00:03:22,850 --> 00:03:28,710 as the value for the length and 12 for the width that's hard coded. 54 00:03:28,710 --> 00:03:33,550 When we gave our function a parameter, what we did was indicate that the function 55 00:03:33,550 --> 00:03:37,160 could accept a value of the type specified. 56 00:03:37,160 --> 00:03:40,060 We also give the parameter a label, length. 57 00:03:40,060 --> 00:03:45,070 This label is simply a constant that we assign this value to. 58 00:03:45,070 --> 00:03:49,850 We can then use this constant inside the function body to refer to that value. 59 00:03:49,850 --> 00:03:54,722 Now, because of the way we've written it to accept a argument or a parameter for 60 00:03:54,722 --> 00:03:59,760 length, the function will not work unless you give the length parameter a value. 61 00:03:59,760 --> 00:04:03,610 And that is how we get a value inside when we're calculating the area. 62 00:04:03,610 --> 00:04:08,150 When we've satisfied these parameter values, the function will execute the code 63 00:04:08,150 --> 00:04:13,980 inside the braces and substitute the value for length with the value that we give. 64 00:04:13,980 --> 00:04:16,010 Now remember, functions are reusable. 65 00:04:16,010 --> 00:04:20,810 And because we accept a length value rather than writing it inside the body, 66 00:04:20,810 --> 00:04:24,020 we can call area again and give it a new length. 67 00:04:24,020 --> 00:04:25,576 We can say 20. 68 00:04:25,576 --> 00:04:27,580 And now you'll see it's been called twice. 69 00:04:27,580 --> 00:04:30,380 This code has been executed twice, but 70 00:04:30,380 --> 00:04:33,620 this time, unlike before, it's not with the same values. 71 00:04:33,620 --> 00:04:37,553 The first time it was executed with a length of 10, and 72 00:04:37,553 --> 00:04:40,408 as you can see, we got an area of 120. 73 00:04:40,408 --> 00:04:43,300 The second time, we passed in 20 for the length. 74 00:04:43,300 --> 00:04:47,520 So this constant now has a value of 20 and our area is 240. 75 00:04:47,520 --> 00:04:51,840 Our function is becoming a lot more reusable. 76 00:04:51,840 --> 00:04:56,680 But it's not all the way there yet because our width value is still fixed. 77 00:04:56,680 --> 00:04:59,200 Don't worry, we can change that, too. 78 00:04:59,200 --> 00:05:01,910 Functions can take multiple parameters. 79 00:05:01,910 --> 00:05:03,940 There's actually no limit. 80 00:05:03,940 --> 00:05:06,900 To specify a second parameter, 81 00:05:06,900 --> 00:05:11,290 we add a comma after the first one, and then we do the same thing again. 82 00:05:11,290 --> 00:05:14,820 We give the second parameter a name, so we'll call it width. 83 00:05:14,820 --> 00:05:20,090 And again we specify the type of the value we're expecting, so we'll say Int. 84 00:05:20,090 --> 00:05:23,130 And now we can again remove the hard coded value. 85 00:05:24,280 --> 00:05:29,450 Like the first time around, we've modified the signature of the function. 86 00:05:29,450 --> 00:05:32,890 So obviously this won't work and we get an error again. 87 00:05:32,890 --> 00:05:34,540 So, let's get rid of this. 88 00:05:36,480 --> 00:05:41,140 And now when we call area, you'll notice it accepts a value for length and width. 89 00:05:41,140 --> 00:05:47,940 So we'll say 10 and 120 or 12, and we'll l get 120, just like before. 90 00:05:47,940 --> 00:05:50,600 Okay, so here's a programmer term for you. 91 00:05:50,600 --> 00:05:54,110 When you specify an input for a function, like we've done over here and 92 00:05:54,110 --> 00:05:58,160 over here, these inputs are called parameters. 93 00:05:58,160 --> 00:06:01,450 When you provide an actual value for a parameter, 94 00:06:01,450 --> 00:06:05,280 that specific value is then called an argument. 95 00:06:05,280 --> 00:06:07,100 So when we are calling the function earlier, 96 00:06:07,100 --> 00:06:09,500 you were only providing a single argument. 97 00:06:09,500 --> 00:06:13,590 But now with our modified function we're providing two arguments. 98 00:06:13,590 --> 00:06:18,460 Okay, so now, we can call this as many times as we want with different values. 99 00:06:18,460 --> 00:06:24,520 So now, we can say area with a length of 15 meters long and 22 meters wide. 100 00:06:24,520 --> 00:06:26,690 And now, the area is 330. 101 00:06:26,690 --> 00:06:30,400 Now, our function is 100% reusable, 102 00:06:30,400 --> 00:06:35,750 given any room with any width and length, we can easily calculate the area. 103 00:06:35,750 --> 00:06:38,430 We don't have to write some formula over and over. 104 00:06:38,430 --> 00:06:41,450 We don't need to specify new length and width constants. 105 00:06:41,450 --> 00:06:44,070 And we don't have to repeat any code. 106 00:06:44,070 --> 00:06:47,370 We've discussed, a few times now, what a function is. 107 00:06:47,370 --> 00:06:49,380 Let's see if this qualifies. 108 00:06:49,380 --> 00:06:52,530 Does it consist of a sequence of instructions that carries out 109 00:06:52,530 --> 00:06:53,790 a specific task? 110 00:06:53,790 --> 00:06:54,990 Well, yes it does. 111 00:06:54,990 --> 00:06:58,620 Ours calculates the area given a length and a width. 112 00:06:58,620 --> 00:06:59,920 Is itself contained? 113 00:06:59,920 --> 00:07:00,850 Yes, it is. 114 00:07:00,850 --> 00:07:04,150 We can call this function as many times as we want, and 115 00:07:04,150 --> 00:07:07,060 it's not tied to a particular room or measurement. 116 00:07:08,650 --> 00:07:12,140 Awesome, this is what a very basic function looks like. 117 00:07:12,140 --> 00:07:15,890 But there's one more feature of functions we need to look at before we can write 118 00:07:15,890 --> 00:07:18,440 any type of function, and we'll do that in the next video.