1 00:00:00,240 --> 00:00:06,270 Up until this point, we've looked at variables in Swift, we've looked at for statements, if statements 2 00:00:06,270 --> 00:00:08,190 and loops of that kind. 3 00:00:09,110 --> 00:00:15,950 What we're missing, though, is a way to organize groups of tasks that we have to run, and the way 4 00:00:15,950 --> 00:00:17,730 we do that is to create a function. 5 00:00:18,260 --> 00:00:25,640 Now, you've already run a function because print with open and close brackets is a function. 6 00:00:26,150 --> 00:00:33,020 You know, it's a function because we have these brackets that open and close and we know it uses data 7 00:00:33,020 --> 00:00:35,230 because we pass data over to it. 8 00:00:36,410 --> 00:00:38,240 That is the hallmark of a good function. 9 00:00:38,840 --> 00:00:42,700 Print does exactly what it says on the tin. 10 00:00:43,160 --> 00:00:45,920 We don't have to give it anything explicit or extra. 11 00:00:46,190 --> 00:00:48,190 We understand what it's going to do. 12 00:00:48,710 --> 00:00:50,530 So let's create our own function. 13 00:00:51,050 --> 00:00:56,090 You define a function with the key word func in swift in other languages. 14 00:00:56,390 --> 00:01:01,760 You might have the full word function or no word for function even. 15 00:01:02,350 --> 00:01:05,850 It depends on what language you have, but all languages have functions. 16 00:01:06,350 --> 00:01:08,630 Now we're going to have a function called Garet. 17 00:01:10,410 --> 00:01:16,250 And in order to define it, to name it, we give it the name of greed and we open and close some brackets, 18 00:01:16,500 --> 00:01:21,780 we'll see what those brackets do a little later on and then we open the curly brace. 19 00:01:23,180 --> 00:01:28,670 And when you press enter and playground's, it automatically adds the closing curly brace for you. 20 00:01:29,150 --> 00:01:31,730 So this is the enclosed inside of here. 21 00:01:33,530 --> 00:01:35,480 And what we're going to do is call. 22 00:01:36,680 --> 00:01:37,970 Print hi there. 23 00:01:39,470 --> 00:01:44,420 So whenever we run this function, we're going to get high there, print it out. 24 00:01:44,960 --> 00:01:45,950 So how do we run it? 25 00:01:46,400 --> 00:01:51,980 Well, you know how to run a print function and running a great function is just the same, except it 26 00:01:51,980 --> 00:01:53,760 doesn't require any data. 27 00:01:54,050 --> 00:01:58,210 If it did, the data it needed would be between those brackets. 28 00:01:58,400 --> 00:02:00,620 We're going to do that, don't worry, a little later on. 29 00:02:01,310 --> 00:02:06,740 So if I run, this great is going to run high there, OK? 30 00:02:06,800 --> 00:02:09,870 Or if you look at the command line, it's printed out at the bottom. 31 00:02:10,670 --> 00:02:13,110 So that's how we create a function and then how we run it. 32 00:02:13,940 --> 00:02:16,940 Now, if I do something like this. 33 00:02:18,920 --> 00:02:20,690 VA eight equals nine. 34 00:02:23,210 --> 00:02:25,770 Ignore that error, in fact, to make the error go away. 35 00:02:25,790 --> 00:02:29,570 We're going to do the following backslash A. 36 00:02:31,880 --> 00:02:37,160 OK, that error should go away now and in fact, we'll change that to let because it's complaining about 37 00:02:37,160 --> 00:02:37,610 the FA. 38 00:02:38,590 --> 00:02:41,710 Can I access the A outside of the function? 39 00:02:42,280 --> 00:02:47,980 No, because when I type it, it doesn't come up in the list and it says cannot find a in scope. 40 00:02:48,490 --> 00:02:50,470 Scope is an important concept. 41 00:02:50,710 --> 00:02:56,020 Scope refers to the area around which you can access certain things. 42 00:02:56,380 --> 00:03:03,130 So inside of a function, if you declare a variable in swift, this is you can only access that variable 43 00:03:03,310 --> 00:03:04,800 inside the function. 44 00:03:04,840 --> 00:03:09,190 You can't access it outside the function, you can't access it pretty much anywhere else. 45 00:03:10,060 --> 00:03:15,990 In some languages you have global variables or you can force global variables, which is an awful idea. 46 00:03:16,180 --> 00:03:19,060 Never, ever do global variables if you can avoid it. 47 00:03:19,060 --> 00:03:22,000 I will admit there are situations where it seems unavoidable. 48 00:03:23,630 --> 00:03:29,480 OK, so that's just a little warning about declaration and scope, so I'm going to remove that for now 49 00:03:29,480 --> 00:03:32,100 and get back onto task with functions. 50 00:03:32,480 --> 00:03:35,030 Now, what if I want to greet someone by name? 51 00:03:35,090 --> 00:03:37,760 Well, let me copy over the following function. 52 00:03:39,630 --> 00:03:41,970 After I fix this, they get. 53 00:03:43,370 --> 00:03:48,800 Paste this in, so now we have another function called greed, which is the same name as this, except 54 00:03:49,130 --> 00:03:51,080 this one takes a name. 55 00:03:51,660 --> 00:03:52,090 OK. 56 00:03:53,700 --> 00:04:02,010 So when we call this function, we pass in the name of Grant so I can actually delete this and that 57 00:04:02,010 --> 00:04:04,850 should work, although I've been having issues with it. 58 00:04:05,550 --> 00:04:08,070 Yeah, this requires a label, so I'm going to undo that. 59 00:04:09,680 --> 00:04:16,430 So sometimes I'm not quite sure why, but it forces you to have a label for what you're passing in, 60 00:04:16,700 --> 00:04:22,550 so we have to give it the label name and say the item you're expecting for that label is Grant. 61 00:04:22,910 --> 00:04:25,460 And then inside the function, we're going to say hello, Grant. 62 00:04:27,290 --> 00:04:28,400 And there we will see it. 63 00:04:29,920 --> 00:04:38,380 OK, so these are called arguments, the data that you pass in is called an argument and multiple are 64 00:04:38,380 --> 00:04:39,670 called arguments. 65 00:04:41,660 --> 00:04:42,740 What else have we got? 66 00:04:43,070 --> 00:04:45,070 Oh, nice, let's go for this one. 67 00:04:46,350 --> 00:04:53,430 So those are arguments and here we might want to get something back because believe it or not, this 68 00:04:53,430 --> 00:04:55,020 might be bad software design. 69 00:04:55,710 --> 00:05:00,780 So the way we get something back is we have great and I've just changed the name of it here. 70 00:05:00,780 --> 00:05:02,490 So it doesn't conflict with this one. 71 00:05:04,060 --> 00:05:06,840 And if we add a little dash and an arrow. 72 00:05:07,770 --> 00:05:13,050 And we give it a type, what you're saying is when you run this function, I'm actually going to give 73 00:05:13,050 --> 00:05:15,900 you something back to whoever's running the function. 74 00:05:17,070 --> 00:05:22,560 So what we do in here is we compile accreting and then we return the greeting. 75 00:05:22,560 --> 00:05:25,340 In the previous lesson, I said we'd get onto this and here it is. 76 00:05:25,830 --> 00:05:29,360 So we are returning a string, which is greeting. 77 00:05:30,510 --> 00:05:38,160 So down here I'm going to call the great function with person grant in it and then that's going to return 78 00:05:38,790 --> 00:05:40,320 a sentence. 79 00:05:40,590 --> 00:05:42,090 So to make this a little clearer. 80 00:05:44,690 --> 00:05:51,650 Let sentence equal, great, so whatever this returns get put, gets put into the variable of sentence. 81 00:05:53,070 --> 00:05:55,740 And then, of course, we can print the sentence. 82 00:05:58,090 --> 00:06:00,310 And everything is as we expected. 83 00:06:00,460 --> 00:06:01,400 Hello, Grant. 84 00:06:01,990 --> 00:06:08,140 So we've passed in data, we've got a return from whatever that function is doing, and then we handle 85 00:06:08,140 --> 00:06:10,570 the return in whatever way we need to. 86 00:06:11,410 --> 00:06:11,640 Right. 87 00:06:11,800 --> 00:06:12,740 What else have we got? 88 00:06:14,460 --> 00:06:18,220 Now, you don't just need to pass in one function. 89 00:06:18,370 --> 00:06:19,830 You can pass in zero. 90 00:06:19,870 --> 00:06:22,390 You can pass in two, you can pass and 50. 91 00:06:23,600 --> 00:06:26,060 So in this case, we're going to pass in a first name. 92 00:06:27,330 --> 00:06:32,950 Separated by a comma and then a last name, and again, we're going to return a string. 93 00:06:33,690 --> 00:06:35,130 We're going to form our greeting. 94 00:06:36,210 --> 00:06:42,330 And we're going to return that greeting and then down here, we're going to print the returned value 95 00:06:42,330 --> 00:06:44,850 from calling this function. 96 00:06:45,960 --> 00:06:54,960 OK, so you're now really getting into the depths of actually how to code you functions are one of the 97 00:06:54,960 --> 00:06:56,220 fundamental building blocks. 98 00:06:56,250 --> 00:06:58,470 They basically organize your code. 99 00:06:58,830 --> 00:06:59,220 Right. 100 00:06:59,400 --> 00:07:01,890 And a bit of a word here about naming things. 101 00:07:02,610 --> 00:07:04,170 These names are all the same. 102 00:07:04,860 --> 00:07:05,250 Great. 103 00:07:06,180 --> 00:07:11,880 But the arguments are different, even if they have a different name, same type, different name, 104 00:07:12,720 --> 00:07:17,400 because Swift is clever enough, we can actually just name them all the same thing. 105 00:07:18,500 --> 00:07:23,450 And in the end, if you type in, where are we, let's go to. 106 00:07:24,450 --> 00:07:29,700 Here, if you type in Greek person, Grant Swift knows that you're looking at that because it says this 107 00:07:29,700 --> 00:07:30,510 one is called name. 108 00:07:30,990 --> 00:07:32,340 This one takes two arguments. 109 00:07:32,370 --> 00:07:33,140 It's not that one. 110 00:07:33,510 --> 00:07:35,170 It's obviously this one. 111 00:07:36,240 --> 00:07:40,950 OK, so it's smart enough to figure that out so you can have functions with all different names. 112 00:07:41,340 --> 00:07:42,930 And where would you have this? 113 00:07:44,420 --> 00:07:52,540 Hmm, say you're making a music app and you pass in a reference to a song to play, you might have to 114 00:07:52,540 --> 00:07:53,470 play functions. 115 00:07:53,470 --> 00:07:59,800 One play takes a euro because you're playing from the Internet and the other play takes a low profile 116 00:07:59,800 --> 00:08:02,170 reference, i.e. a file type. 117 00:08:05,590 --> 00:08:12,160 The system knows which one you've passed in, so it knows which play function to activate. 118 00:08:12,550 --> 00:08:15,090 OK, so that's why you'd use a practical example of that. 119 00:08:15,520 --> 00:08:16,890 Not that this isn't practical. 120 00:08:17,270 --> 00:08:18,400 Same same thing, really. 121 00:08:19,210 --> 00:08:20,910 It knows these things. 122 00:08:20,920 --> 00:08:25,780 Now, the technical word for this is called a signature, right? 123 00:08:25,900 --> 00:08:27,410 Because the signature is unique. 124 00:08:27,430 --> 00:08:30,350 If you sign your name, everyone knows it's you. 125 00:08:30,850 --> 00:08:32,570 Functions have signatures. 126 00:08:32,710 --> 00:08:35,380 In fact, many things in coding have signatures. 127 00:08:35,590 --> 00:08:38,800 And signature just represents what that thing looks like. 128 00:08:39,530 --> 00:08:41,350 OK, I think that's it for that. 129 00:08:42,130 --> 00:08:46,280 How about some homework actually before the homework? 130 00:08:46,300 --> 00:08:47,710 One more thing on naming. 131 00:08:49,140 --> 00:08:57,840 This function does something, it's a doing object, therefore functions should generally have a verb 132 00:08:57,840 --> 00:09:03,900 attached to them, i.e. play great work. 133 00:09:05,030 --> 00:09:06,750 Turn screen off, right? 134 00:09:06,770 --> 00:09:13,280 That's really specific, but your function should be well described, so whoever's using it knows what 135 00:09:13,280 --> 00:09:16,300 they're looking at and they know what it's going to do. 136 00:09:16,460 --> 00:09:18,950 And that person includes yourself in six months time. 137 00:09:19,130 --> 00:09:21,890 When you look at this code and go, what moron wrote this? 138 00:09:22,220 --> 00:09:22,420 Right. 139 00:09:22,520 --> 00:09:24,920 Because that happens all the time happens to me. 140 00:09:25,640 --> 00:09:30,650 You just think, why didn't I name these things properly so I can save some time at this point. 141 00:09:30,770 --> 00:09:32,510 All right, let's get on to the homework. 142 00:09:34,000 --> 00:09:41,320 I'd like you to create a function that takes three numbers, return those numbers, each one multiplied 143 00:09:41,320 --> 00:09:43,030 by three as an array. 144 00:09:43,580 --> 00:09:49,660 So you are going to return an array and I'm going to give you the first line. 145 00:09:52,360 --> 00:09:55,630 Of the function, so this is the base signature of the function. 146 00:09:57,240 --> 00:10:02,970 It's called multiply, we pass in an array of integers and we return an array of integers. 147 00:10:04,280 --> 00:10:12,200 What that array of integers you return is all of this array of integers, each one multiplied by three.