0 1 00:00:00,150 --> 00:00:05,130 Welcome to another lesson on Swift Deep Dive. In today's lesson, 1 2 00:00:05,130 --> 00:00:11,430 we're going to talk more about functions. And we've already seen functions in action, so we know that 2 3 00:00:11,430 --> 00:00:14,250 there are two stages to any function. 3 4 00:00:14,250 --> 00:00:20,910 First, the part where we create the function, where we have the "func" keywords and we specify the name 4 5 00:00:20,910 --> 00:00:25,170 of the function, and then we describe what the function should do. 5 6 00:00:25,380 --> 00:00:31,590 And then there's a second part where some point down the line where we call the function and the computer 6 7 00:00:31,590 --> 00:00:37,830 will look to see where our function was defined, and do all of the stuff that we said it should. 7 8 00:00:38,610 --> 00:00:39,920 So what is the problem 8 9 00:00:39,930 --> 00:00:42,220 that a function actually solves? 9 10 00:00:42,230 --> 00:00:50,310 Well, let's say that you had a little robot that you bought and you need it to fetch some milk from the 10 11 00:00:50,310 --> 00:00:57,450 store. So you would have to tell it a whole bunch of instructions, say, you had to do it via print statements, 11 12 00:00:57,900 --> 00:01:04,020 you would have to tell it to leave the house, and then move right two blocks, move up four blocks, move 12 13 00:01:04,020 --> 00:01:11,160 right two blocks again, and then buy the milk, and then all the way back with all the instructions in 13 14 00:01:11,160 --> 00:01:13,740 order to get your milk. 14 15 00:01:13,770 --> 00:01:20,410 So if you had to do this day after day, then your fingers would hurt from typing literally. 15 16 00:01:20,520 --> 00:01:22,710 So how can we make this better? 16 17 00:01:22,710 --> 00:01:31,200 Well, what if we could take all of these instructions and packaged them together into a function. 17 18 00:01:31,200 --> 00:01:38,670 And this is essentially what our functions do. It packages a whole bunch of instructions into a block 18 19 00:01:38,670 --> 00:01:44,130 of code which can be triggered simply by calling the function name. 19 20 00:01:44,580 --> 00:01:50,880 So now we can identify all of these instructions by a simple and descriptive name. 20 21 00:01:51,030 --> 00:01:54,390 So the next time you want your robot to buy you some milk, 21 22 00:01:54,390 --> 00:02:00,810 you can simply just call getMilk and it will be able to look up what it needs to do through the 22 23 00:02:00,810 --> 00:02:06,770 getMilk function, and then be able to perform that task much shorter and much less typing. 23 24 00:02:06,780 --> 00:02:12,110 So here are the two parts of the function, the part where we create the function, it looks like this, and 24 25 00:02:12,120 --> 00:02:17,280 the part where we call the function, it looks like this, where we simply specify the name of the function. 25 26 00:02:17,310 --> 00:02:24,420 So, in this case, it's called getMilk, and then we add a pair of parentheses which are empty. And a little 26 27 00:02:24,420 --> 00:02:30,180 bit later on, when we look at more complex functions, we'll learn what we can put inside these parentheses. 27 28 00:02:30,540 --> 00:02:36,150 But if we simply just want to trigger a block of code like this, then we just need to call our function 28 29 00:02:36,270 --> 00:02:37,800 like this. 29 30 00:02:37,800 --> 00:02:43,350 Now, if you open up your playground again or simply create a new playground if you can't find the other 30 31 00:02:43,350 --> 00:02:46,700 one, then delete everything that's currently inside 31 32 00:02:46,740 --> 00:02:51,600 to start with a clean slate and we're going to create a function of our own. 32 33 00:02:51,690 --> 00:02:57,870 We're going to use the func keyword to specify that we're creating a new function, and then we're going 33 34 00:02:57,870 --> 00:02:59,760 to give that function a name, 34 35 00:02:59,760 --> 00:03:05,400 so we're going to call it greeting. And then we're going to add a set of parentheses and then a set of 35 36 00:03:05,400 --> 00:03:08,940 curly braces if we break this line down, 36 37 00:03:08,940 --> 00:03:11,950 This is the keyword that creates a new function. 37 38 00:03:11,970 --> 00:03:19,020 This is the name that we've given the function. The round of braces or the parentheses is where we can 38 39 00:03:19,020 --> 00:03:21,340 put some potential inputs 39 40 00:03:21,660 --> 00:03:26,100 and the curly braces is where the body of the function will go. 40 41 00:03:26,130 --> 00:03:33,060 So when this function is triggered, it's going to look inside these curly braces to see what code needs 41 42 00:03:33,060 --> 00:03:35,030 to get executed. 42 43 00:03:35,100 --> 00:03:40,380 Now, more commonly, you'll see a function formatted like this. 43 44 00:03:40,380 --> 00:03:47,520 And in fact, every time you create a new function, as soon as you open up the open curly brace, if you 44 45 00:03:47,520 --> 00:03:55,440 just hit enter, Xcode will automatically insert the closing brace for you in the right position. 45 46 00:03:55,530 --> 00:04:02,310 And this makes it much easier to avoid these missing curly braces and make your code a lot easier to 46 47 00:04:02,310 --> 00:04:02,720 read. 47 48 00:04:03,840 --> 00:04:11,040 Let's say that in our function when it gets triggered, it's going to print "hello" to the debug console. 48 49 00:04:11,040 --> 00:04:19,110 So if we go here click on it and hold and change to manually run, I can manually select which lines of 49 50 00:04:19,110 --> 00:04:21,870 code I want to run until. 50 51 00:04:21,870 --> 00:04:28,230 So if I click on, say, line six or line seven, and it's going to run all of the lines above. 51 52 00:04:28,230 --> 00:04:33,430 Now, you might expect something to happen, but nothing happened. 52 53 00:04:33,510 --> 00:04:34,720 Why? 53 54 00:04:34,860 --> 00:04:36,970 There's nothing wrong with your computer. 54 55 00:04:36,990 --> 00:04:41,800 It's actually because it's not enough to just create a function. 55 56 00:04:41,850 --> 00:04:45,750 You also have to trigger it or execute it. 56 57 00:04:46,280 --> 00:04:49,830 And in programming, we call that calling the function. 57 58 00:04:50,580 --> 00:04:59,730 So on line six or seven, we can call the function by simply tapping in into its name. And you can see that 58 59 00:04:59,870 --> 00:05:07,160 as soon as I do that, we get a little "f" symbol here, "f," a function show up and it shows us that "Hey, did 59 60 00:05:07,160 --> 00:05:09,890 you mean to call the greeting function?" 60 61 00:05:10,010 --> 00:05:11,180 And, yeah, I did. 61 62 00:05:11,180 --> 00:05:16,720 So I'm gonna hit enter and it automatically inserts the rest of the code for me. 62 63 00:05:16,730 --> 00:05:23,510 So now if we try to run our code and including the line where we called our function, well, then you'll 63 64 00:05:23,510 --> 00:05:31,190 see it actually trigger all the code in here and the word "hello" actually gets printed. 64 65 00:05:31,190 --> 00:05:38,210 Remember, the funky word is used to create a set of instructions and package it with a name, and the actual 65 66 00:05:38,210 --> 00:05:46,660 name plus the parentheses is where you actually activate or call that function. Here's a challenge. 66 67 00:05:46,660 --> 00:05:49,580 Change the code so that you get the word "hello" 67 68 00:05:49,600 --> 00:05:51,410 printed four times. 68 69 00:05:51,680 --> 00:05:53,240 Now, there's a few ways of doing this. 69 70 00:05:53,260 --> 00:05:58,240 And as long as you can get the words "hello" printrf down here four times, then you can consider yourself 70 71 00:05:58,240 --> 00:05:58,930 successful. 71 72 00:06:02,370 --> 00:06:03,130 Now, 72 73 00:06:03,160 --> 00:06:11,740 one way of doing this is to simply copy our print statement and have it execute four times when we trigger 73 74 00:06:11,800 --> 00:06:16,180 the greeting function, that will get us our "hello" 74 75 00:06:16,210 --> 00:06:17,550 printed four times. 75 76 00:06:17,710 --> 00:06:23,910 But the alternative is we could have also just called our function four times, right? 76 77 00:06:24,100 --> 00:06:30,250 So if I press play, you can see that this line goes to find greeting, prints "hello." 77 78 00:06:30,250 --> 00:06:37,060 second line print "hello," third line print "hello," and so on. We get the same effect. Now, the last thing 78 79 00:06:37,060 --> 00:06:44,110 I want to talk about before we move on from functions is this idea of scope. Scope is really important 79 80 00:06:44,200 --> 00:06:52,480 because it describes when a function can be accessed or can be used. Just as if I have an apple tree 80 81 00:06:52,600 --> 00:06:58,920 in my garden, then only myself or members of my family can actually pick apples from the tree. 81 82 00:06:59,500 --> 00:07:05,830 Whereas if that apple tree was in the neighborhood, well, then everybody, all my neighbors, everybody on the 82 83 00:07:05,830 --> 00:07:07,720 street can pick from that apple tree. 83 84 00:07:08,410 --> 00:07:15,580 So here I've got two functions, one called greeting1 and the other called greeting2, and they do 84 85 00:07:15,580 --> 00:07:16,980 something slightly different. 85 86 00:07:16,990 --> 00:07:25,870 This one prints "hello" and this one prints "hey." Now, inside each of these curly braces, it's a block of code. 86 87 00:07:26,350 --> 00:07:30,160 So you can view these as the walls of a house. 87 88 00:07:30,730 --> 00:07:39,520 So that means that if I was to create a variable, say, inside greeting1, let's call it myName and we'll 88 89 00:07:39,520 --> 00:07:40,990 set it to equal "Angela," 89 90 00:07:41,650 --> 00:07:47,040 I can actually only use this variable myName inside greeting1. 90 91 00:07:47,050 --> 00:07:50,260 So let's try and print myName. 91 92 00:07:50,260 --> 00:08:00,500 That is perfectly acceptable. But if I try to do this inside greeting2 and I try to run it, then there's 92 93 00:08:00,520 --> 00:08:05,680 a problem here because this myName is an unresolved identifier. 93 94 00:08:05,700 --> 00:08:13,620 It doesn't know what it is because it was created within the four walls of greeting1. 94 95 00:08:13,720 --> 00:08:20,640 So it's only available for use inside these curly braces where it was created. 95 96 00:08:20,740 --> 00:08:31,930 Similarly, if I had created my greeting2 inside the boundaries of my greeting1 function, so greeting2 96 97 00:08:31,930 --> 00:08:35,740 is now nested inside greeting1, 97 98 00:08:35,740 --> 00:08:43,120 well, now I can't actually call greeting2 outside of this boundary. 98 99 00:08:43,960 --> 00:08:47,970 So this doesn't work and it'll give me an error. 99 100 00:08:48,190 --> 00:08:55,490 And the only way I can trigger it is by moving it inside of the block where it was created. 100 101 00:08:55,570 --> 00:08:58,440 So namely right here. 101 102 00:08:58,510 --> 00:09:03,760 So these curly braces matter a huge deal. 102 103 00:09:03,760 --> 00:09:09,760 And that's why when you're coding, especially when you're creating functions, it's really important to 103 104 00:09:09,760 --> 00:09:17,200 keep them indented in such a way that you can always follow the closing brace vertically up to the line 104 105 00:09:17,260 --> 00:09:21,010 where the opening brace is located. 105 106 00:09:21,010 --> 00:09:22,090 There's a couple of tricks here. 106 107 00:09:22,090 --> 00:09:26,860 You can double click on the closing brace and it will highlight the entire block for you. 107 108 00:09:27,340 --> 00:09:33,040 Alternatively, you can find a closing brace and hit the left and right key on it, and it will flash the 108 109 00:09:33,040 --> 00:09:38,080 matching opening brace just to show you where the boundaries lie. 109 110 00:09:38,620 --> 00:09:48,310 If you have your indentation really off and you're a bit messy, it might help to hit command A to select 110 111 00:09:48,310 --> 00:09:55,400 everything, and then go to Editor, Structure, and click on Re-indent 111 112 00:09:55,580 --> 00:10:01,590 for these closing braces to be placed in the position where they are supposed to. 112 113 00:10:01,840 --> 00:10:07,790 And as you get used to this way of indenting closing braces, at some point, your eye will be able to 113 114 00:10:07,790 --> 00:10:11,200 see easily that this is a block of code 114 115 00:10:11,200 --> 00:10:14,810 and this is another block of code. 115 116 00:10:14,810 --> 00:10:22,730 The best way of making sure that your indentation is correct is whenever you find yourself creating 116 117 00:10:22,820 --> 00:10:24,360 a opening brace, 117 118 00:10:24,500 --> 00:10:33,170 simply hit enter because Xcode will then automatically insert the matching closing brace. In that way, 118 119 00:10:33,260 --> 00:10:35,480 you'll always have the proper indentation. 119 120 00:10:36,770 --> 00:10:40,810 So that's all on the most basic type of functions. 120 121 00:10:40,820 --> 00:10:48,200 The next thing to do is: complete the Function 1 assignment.