1 00:00:00,450 --> 00:00:04,783 Let's create a new playground for this course and call it SwiftFunctions. 2 00:00:04,783 --> 00:00:08,662 So, if you've created a new one, get rid of all the code in there. 3 00:00:08,662 --> 00:00:10,290 Let's say we're writing app for 4 00:00:10,290 --> 00:00:13,150 a contractor who's job is to lay down carpet in new homes. 5 00:00:14,360 --> 00:00:19,440 To know how much carpet he or she needs, we need to calculate the area of the room. 6 00:00:19,440 --> 00:00:23,760 To keep this simple we'll assume all these rooms are perfect rectangles. 7 00:00:23,760 --> 00:00:26,970 So, we just need the length and width of the room. 8 00:00:26,970 --> 00:00:30,884 The first room that we're working on has a length of 10 meters or feet, 9 00:00:30,884 --> 00:00:34,380 use whatever unit of measurement you prefer. 10 00:00:34,380 --> 00:00:39,477 So let's create a constant, we'll say let length equal 10. 11 00:00:39,477 --> 00:00:43,973 Let's also at a constant to hold a value for the width of the room. 12 00:00:43,973 --> 00:00:48,200 So I'll say let width equal 12. 13 00:00:48,200 --> 00:00:53,108 The area of a rectangular room is defined as length times width. 14 00:00:53,108 --> 00:00:55,559 So, we can calculate the area pretty easily. 15 00:00:55,559 --> 00:00:59,830 Say let area equal length multiplied by width, 16 00:00:59,830 --> 00:01:07,007 assuming these measurements are in meters, our area is 120 square meters. 17 00:01:07,007 --> 00:01:09,058 Okay, so room number one, check. 18 00:01:09,058 --> 00:01:12,218 We have an area for our contractor to use. 19 00:01:12,218 --> 00:01:13,682 But there's a second room in this house. 20 00:01:13,682 --> 00:01:15,614 So, let's write some more code. 21 00:01:15,614 --> 00:01:19,577 Let's go back up to the top and we'll add a comment, 22 00:01:19,577 --> 00:01:23,100 we'll say this is area for area calculation. 23 00:01:25,439 --> 00:01:27,279 For room number one. 24 00:01:29,411 --> 00:01:36,620 Okay, we have a second room as well so we'll say, area calculation for room two. 25 00:01:36,620 --> 00:01:39,961 To get the area for room two, we need another set of measurements, so 26 00:01:39,961 --> 00:01:41,064 we'll write that in. 27 00:01:41,064 --> 00:01:48,113 We'll say let's second length equal 14, 28 00:01:48,113 --> 00:01:52,386 and second width equal 8. 29 00:01:52,386 --> 00:01:55,897 The new area would simply be the product of these two numbers. 30 00:01:55,897 --> 00:02:00,641 So let second area equal second 31 00:02:00,641 --> 00:02:05,022 length times second width. 32 00:02:05,022 --> 00:02:07,851 Okay, let's take a step back and examine this. 33 00:02:07,851 --> 00:02:13,347 We have code that does the exact same thing here, but it just does it twice. 34 00:02:13,347 --> 00:02:16,150 We're violating a fundamental principle. 35 00:02:16,150 --> 00:02:18,980 DRY, or don't repeat yourself. 36 00:02:18,980 --> 00:02:23,101 Whenever you write code that does the exact same thing twice, 37 00:02:23,101 --> 00:02:26,125 it's a good idea to make a function out of it. 38 00:02:26,125 --> 00:02:28,136 Let's write our first function. 39 00:02:28,136 --> 00:02:32,602 To write a function we have a set of specific syntax. 40 00:02:32,602 --> 00:02:37,111 So we start with the keyword func, which is short for function. 41 00:02:37,111 --> 00:02:39,727 Then, we give our function a name. 42 00:02:39,727 --> 00:02:44,274 The naming rules for functions are similar to those for variables and constants. 43 00:02:44,274 --> 00:02:49,276 If you've forgotten, those basic rules are, we're using camel casing, 44 00:02:49,276 --> 00:02:54,133 no whitespace, and we don't start with numbers or special characters. 45 00:02:54,133 --> 00:02:56,817 So let's call our function area. 46 00:02:56,817 --> 00:02:58,869 The next part is important. 47 00:02:58,869 --> 00:03:01,029 After the name of the function, 48 00:03:01,029 --> 00:03:05,595 without any spaces, add a set of opening and closing parentheses. 49 00:03:05,595 --> 00:03:09,472 After that, just like with control statements, 50 00:03:09,472 --> 00:03:14,102 we use a set of curly braces to open the body of the function. 51 00:03:14,102 --> 00:03:16,372 So inside the body of this function, 52 00:03:16,372 --> 00:03:21,437 let's copy paste the first three lines of code calculating the area for room one. 53 00:03:21,437 --> 00:03:23,715 Now we have an error here really quick, and 54 00:03:23,715 --> 00:03:26,258 that's because we have two things named area. 55 00:03:26,258 --> 00:03:29,142 So at the top will say area of room. 56 00:03:29,142 --> 00:03:33,960 Okay, now inside the body of this function let's get those three 57 00:03:33,960 --> 00:03:39,138 lines that we used to calculate the area for room one and put it in here. 58 00:03:39,138 --> 00:03:40,977 I'm just going to type it out again. 59 00:03:40,977 --> 00:03:47,073 So, we'll say let length equal 10, 60 00:03:47,073 --> 00:03:52,245 let width equal 12, let area of 61 00:03:52,245 --> 00:03:57,249 room equal length times width. 62 00:03:57,249 --> 00:04:02,941 And now I'm going to get rid of this, and I'll just move this comment over. 63 00:04:08,177 --> 00:04:13,017 Let's also add a print statement inside the function to print the value 64 00:04:13,017 --> 00:04:16,741 of the area we just calculated, so print area of room. 65 00:04:16,741 --> 00:04:21,690 So, what we just did here, this block of code is a function. 66 00:04:22,710 --> 00:04:27,319 A function is a unit of code that performs a specific task, 67 00:04:27,319 --> 00:04:30,151 in this case calculating the area. 68 00:04:30,151 --> 00:04:35,162 Now, rather than writing these exact same lines of code over and over again, 69 00:04:35,162 --> 00:04:39,729 we can simply write out the function name to execute the lines of code. 70 00:04:39,729 --> 00:04:45,131 So, right below here I can type on area, just like you see here, 71 00:04:45,131 --> 00:04:52,459 and you'll notice in the results area that this code has been evaluated and executed. 72 00:04:52,459 --> 00:04:56,934 You'll see that the value of the area of the room has been printed out. 73 00:04:56,934 --> 00:05:01,070 Writing out a function name to execute the code inside, 74 00:05:01,070 --> 00:05:04,681 like we just did, is called calling a function. 75 00:05:04,681 --> 00:05:08,945 We can call this function over and over again as many times as we want and 76 00:05:08,945 --> 00:05:14,550 you'll see that these exact lines of code are executed the same number of times. 77 00:05:14,550 --> 00:05:19,397 Functions are really useful because it allows us to create these blocks 78 00:05:19,397 --> 00:05:24,324 of code that encapsulate certain functionality that we can reuse as many 79 00:05:24,324 --> 00:05:27,814 times as we want without having to write more code. 80 00:05:27,814 --> 00:05:30,938 Unfortunately there's a limitation with our code. 81 00:05:30,938 --> 00:05:34,569 You'll notice that every time we call the function, 82 00:05:34,569 --> 00:05:39,489 the value of area is always 120 square meters, because the values for 83 00:05:39,489 --> 00:05:44,588 the length and width are hard coded, meaning that they cannot be changed. 84 00:05:44,588 --> 00:05:48,392 What if we wanted to calculate the area for a different room? 85 00:05:48,392 --> 00:05:50,361 Let's check that out in the next video. 86 00:05:50,361 --> 00:05:53,864 But before we move on, I want to make sure you've understood the syntax. 87 00:05:53,864 --> 00:05:57,152 So let's work on a quick code challenge before we proceed.