0 1 00:00:00,570 --> 00:00:02,750 Welcome to another Swift Deep Dive. 1 2 00:00:02,760 --> 00:00:09,380 The goal of this lesson is to really understand the Swift syntax behind functions with inputs. 2 3 00:00:09,760 --> 00:00:15,300 Our current problem is that when our xylophone key is pressed, we want to pass over the title of the 3 4 00:00:15,300 --> 00:00:19,220 button to our play sound function. To solve this problem, 4 5 00:00:19,230 --> 00:00:24,010 we have to learn about function parameters, arguments, and data types. 5 6 00:00:24,020 --> 00:00:30,990 Now, if you think back to the greeting function that we created earlier on, all it does is say "Hello!" 6 7 00:00:30,990 --> 00:00:35,260 But what if we wanted it to do something slightly different every time? 7 8 00:00:35,370 --> 00:00:41,610 What if, say, we wanted it to greet whoever came to our house, so it would have to say "Hello, Angela!" or 8 9 00:00:41,610 --> 00:00:45,150 "Hello, Jack!" depending on who's actually at the door, 9 10 00:00:45,240 --> 00:00:51,660 how can we pass the name of that person to the greeting function as an input? 10 11 00:00:51,660 --> 00:00:59,940 Well, this is how we might create a Swift function that can take an input, so that way, the input can be 11 12 00:00:59,940 --> 00:01:05,200 incorporated into the body of the function to do something different every time. 12 13 00:01:05,400 --> 00:01:13,020 In programming lingo, the input is called a parameter and the parameter is a bit like a variable without 13 14 00:01:13,050 --> 00:01:13,770 a value. 14 15 00:01:14,550 --> 00:01:20,060 All we have to do to create it is to give it a name and then give it a data type. 15 16 00:01:20,580 --> 00:01:27,150 So here would be the name of a parameter and then it would be a colon, and then the DataType of that 16 17 00:01:27,150 --> 00:01:28,680 parameter. 17 18 00:01:28,680 --> 00:01:35,150 Previously, when we were creating variables or constants, we've kind of just ignored their data type. 18 19 00:01:35,400 --> 00:01:41,610 But in fact, every variable in Swift gets assigned a data type when it's created. 19 20 00:01:41,610 --> 00:01:46,500 This means that the variable can only hold data of a particular type. 20 21 00:01:47,040 --> 00:01:53,220 So in this case, we have a variable code Angela that's holding a Int type data. 21 22 00:01:53,520 --> 00:02:03,990 So you could also create this like so. And here, after a colon, we specify the DataType of the data that's 22 23 00:02:03,990 --> 00:02:06,390 going into this variable. 23 24 00:02:06,480 --> 00:02:13,750 Now, how come we were able to create our variable like this without assigning the data type? 24 25 00:02:13,890 --> 00:02:21,540 Well, Swift also has a capability called Type Inference which means that it's able to infer the DataType 25 26 00:02:21,540 --> 00:02:25,630 based on the data that you've put into the variable. 26 27 00:02:25,650 --> 00:02:29,720 So in this case, our data is a whole bunch of whole numbers, 27 28 00:02:29,730 --> 00:02:30,080 right? 28 29 00:02:30,090 --> 00:02:33,390 There's no decimal places and there's no text. 29 30 00:02:33,510 --> 00:02:40,310 So this has assigned the type or the DataType of an Int. 30 31 00:02:40,380 --> 00:02:45,930 Now, the thing about the data types are that they have to stay the same. 31 32 00:02:46,020 --> 00:02:49,230 So if you create a variable that's meant to hold integers, 32 33 00:02:49,290 --> 00:02:53,370 well, it can only ever hold integers in its entire lifetime. 33 34 00:02:54,000 --> 00:03:01,050 And if you try to fit a different type into the variable that holds integers, 34 35 00:03:01,050 --> 00:03:02,620 well, it's not going to fit. 35 36 00:03:03,060 --> 00:03:08,850 It's just like those children's toys which you have to fit a particular shape through a particular hole. 36 37 00:03:09,060 --> 00:03:14,010 And if you try to fit a star through a square, then it's not going to work. 37 38 00:03:14,010 --> 00:03:17,580 Let's see Swift data types and type inference in action. 38 39 00:03:17,610 --> 00:03:23,490 Let's go ahead and create a new variable and I'm going to call it myAge and I'm going to set it to 39 40 00:03:23,490 --> 00:03:25,150 equal twelve. 40 41 00:03:25,170 --> 00:03:33,480 Now, in this case, because we made this variable myAge, store a piece of data that is a whole number, then 41 42 00:03:33,540 --> 00:03:41,220 myAge actually gets assigned the data type of int. And we can confirm this by holding down the option 42 43 00:03:41,220 --> 00:03:44,090 key and clicking on the name of our variable. 43 44 00:03:44,310 --> 00:03:53,560 And you can see that this is the DataType, that it managed to infer from our value right here. Remember 44 45 00:03:53,560 --> 00:04:00,580 that we can change the data that's stored inside a variable by simply setting it to equal a new piece 45 46 00:04:00,580 --> 00:04:04,830 of data, but we can never change its data type. 46 47 00:04:04,840 --> 00:04:11,110 So if I decided this was going to be equal to three, then this is not going to work. It's going to give 47 48 00:04:11,110 --> 00:04:18,200 me an error and it's going to say that "Cannot assign value of type 'String' to type 'Int.' 48 49 00:04:18,400 --> 00:04:21,210 So myAge is of type int "Three." 49 50 00:04:21,220 --> 00:04:23,790 This text here is of type 'String.' 50 51 00:04:23,800 --> 00:04:27,100 So there's a mismatch on this line right here. 51 52 00:04:27,190 --> 00:04:33,790 Now, if you change the data type of the variable when it was first created, then it wouldn't give you 52 53 00:04:33,790 --> 00:04:34,920 any errors. 53 54 00:04:35,080 --> 00:04:40,480 It would be able to accept a data type of string. 54 55 00:04:40,480 --> 00:04:46,990 But now if we hold down ALT and we click on this, it's now assigned a data type of String and it can 55 56 00:04:46,990 --> 00:04:50,460 forever only hold string data. 56 57 00:04:50,470 --> 00:04:53,840 So this will now give us an error. 57 58 00:04:54,010 --> 00:05:00,970 Now, instead of letting Swift infer which data type we want, we can also assign it at the point where we 58 59 00:05:00,970 --> 00:05:07,610 create the variable. And to do that, we add a colon, and then we specify the data type. 59 60 00:05:07,630 --> 00:05:13,380 So in this case, it would be a String and all the data types start with a capital letter. 60 61 00:05:13,630 --> 00:05:18,270 So we could say it's a String or we could say it has to hold an Int. 61 62 00:05:18,310 --> 00:05:23,050 Now, notice how the assigned data type will override whatever value you put in there. 62 63 00:05:23,500 --> 00:05:30,020 So this line has a complete mismatch and we get an error. Because this myAge is, of course , of type Int, 63 64 00:05:30,160 --> 00:05:31,900 because that's what we said it would be. 64 65 00:05:32,080 --> 00:05:39,730 And then we tried to make it hold a string or text data, so that doesn't work, it has to be matching with 65 66 00:05:39,730 --> 00:05:40,330 the data type 66 67 00:05:40,330 --> 00:05:45,990 we specified. So now that we've understood about these data types, 67 68 00:05:45,990 --> 00:05:49,610 let's go back to creating a function with an input. 68 69 00:05:49,860 --> 00:05:56,700 When we create our function, we specify the data type of the input that we're expecting. And when we call 69 70 00:05:56,700 --> 00:05:59,880 the function, we call the same function name. 70 71 00:05:59,970 --> 00:06:05,630 And then in between the round brackets, we specify the input, or in programming, 71 72 00:06:05,760 --> 00:06:07,980 it's called the argument. 72 73 00:06:07,980 --> 00:06:12,530 So the parameter is the name of the input. 73 74 00:06:12,570 --> 00:06:21,340 The argument is the value of the input that we actually pass over when we call or trigger the function. 74 75 00:06:21,870 --> 00:06:25,860 Let's create another function called greeting2 75 76 00:06:26,070 --> 00:06:33,430 and this function is going to have an input parameter in between these round brackets. 76 77 00:06:33,510 --> 00:06:42,960 The input is going to be called whoToGreet and it's not going to have a value, but it does need a data 77 78 00:06:42,960 --> 00:06:43,430 type. 78 79 00:06:43,800 --> 00:06:49,470 So the whoToGreet which is the name of the person that this function has to incorporate is going to 79 80 00:06:49,470 --> 00:06:52,920 be a String data type. And that's it. 80 81 00:06:53,130 --> 00:06:59,070 So this is almost like what we did for creating a variable other than giving it a value, 81 82 00:06:59,070 --> 00:07:02,040 and we're not using that var keyword. 82 83 00:07:02,190 --> 00:07:07,230 Now, we're planning ahead. When this greeting2 gets triggered, 83 84 00:07:07,230 --> 00:07:08,430 what do we want to happen? 84 85 00:07:08,430 --> 00:07:10,730 Well, we want to somehow incorporate this 85 86 00:07:10,770 --> 00:07:12,680 whoToGreet variable. 86 87 00:07:12,930 --> 00:07:19,400 So let's say that we had a printed message which just says "Hello," 87 88 00:07:19,560 --> 00:07:24,750 and then it says the "Hello" to the whoToGreet. 88 89 00:07:24,750 --> 00:07:32,770 But, of course, in order to insert a variable into our string, we have to use our string interpolation, 89 90 00:07:33,000 --> 00:07:36,780 and then we can write whoToGreet inside. 90 91 00:07:36,780 --> 00:07:43,710 So now if we call this function greeting2, you can see that it automatically inserts the name of our 91 92 00:07:43,710 --> 00:07:45,250 parameter whoToGreet 92 93 00:07:45,510 --> 00:07:52,080 and then here we've got a little place holder to specify the value. And whatever we put in here is going 93 94 00:07:52,080 --> 00:07:57,420 to be assigned to this variable and then it's going to be printed out right here. 94 95 00:07:57,420 --> 00:08:01,200 So let's put in a value of "Angela." 95 96 00:08:01,200 --> 00:08:07,750 Now, if I run all of my code and this line triggers, then it's going to print "Hello," 96 97 00:08:07,890 --> 00:08:10,080 and then it's going to add that name Angela. 97 98 00:08:10,110 --> 00:08:15,380 So that Angela got added in as the input when we called greeting2, 98 99 00:08:15,420 --> 00:08:19,550 so it moves to be the value of this parameter 99 100 00:08:19,560 --> 00:08:20,530 whoToGreet, 100 101 00:08:20,790 --> 00:08:28,560 and then it gets used inside the body of the function. Now try calling this greeting2 function that 101 102 00:08:28,560 --> 00:08:37,360 you've created with some different values and see what you get. So I could say greeting2 102 103 00:08:37,950 --> 00:08:40,440 and I could say "Jack Bauer." 103 104 00:08:40,890 --> 00:08:44,150 And now if we run our code, you'll see that 104 105 00:08:44,160 --> 00:08:44,650 it'll write 105 106 00:08:44,670 --> 00:08:45,510 "Hello Angela," 106 107 00:08:45,540 --> 00:08:51,620 "Hello Jack Bauer." Our greeting function is no longer always doing the same thing anymore. 107 108 00:08:51,630 --> 00:08:58,860 Instead, it's adapting to whatever input we give it and it's able to achieve different in outcomes. 108 109 00:08:58,860 --> 00:09:05,370 So even though, once we've created a function, we can't really change what's going to happen with that 109 110 00:09:05,370 --> 00:09:06,860 function ever again, 110 111 00:09:06,990 --> 00:09:09,300 but we can still make it do different things 111 112 00:09:09,300 --> 00:09:16,380 as long as we provide different inputs. Function with the input vastly improved the reuse ability of 112 113 00:09:16,380 --> 00:09:20,160 our functions by providing parts that can be modified. 113 114 00:09:20,250 --> 00:09:25,890 So if you think back to the analogy of our housekeeping robot, it'd be really useless if it could only 114 115 00:09:25,890 --> 00:09:34,380 ever fetch milk. If we had an item to fetch input, then we can tell it to get bread one day, and then newspapers 115 116 00:09:34,380 --> 00:09:40,380 the next day, and we can use that input within the body of the function. 116 117 00:09:40,510 --> 00:09:46,900 So now that you've seen how you can create functions with inputs and how you can call them later on, 117 118 00:09:47,470 --> 00:09:53,690 it's time to complete the Functions 2 coding exercise and complete that assignment. 118 119 00:09:53,860 --> 00:09:57,610 assignment.