0 1 00:00:00,180 --> 00:00:00,700 Hey, guys. 1 2 00:00:00,720 --> 00:00:03,300 Welcome to another Swift Deep Dive. 2 3 00:00:03,300 --> 00:00:08,070 And in this Deep Dive, we're going to talk about functions that can have outputs. 3 4 00:00:08,700 --> 00:00:12,270 So far, we've already seen two types of functions. 4 5 00:00:12,510 --> 00:00:19,380 The most basic or the vanilla version if you will is simply where we package a bunch of code together 5 6 00:00:19,860 --> 00:00:21,570 and we execute them 6 7 00:00:21,600 --> 00:00:23,940 line by line in the same order. 7 8 00:00:23,940 --> 00:00:29,940 So that saves us from typing lots of code repeatedly, and especially when we have the functionality that 8 9 00:00:29,940 --> 00:00:32,370 needs to be executed multiple times. 9 10 00:00:32,520 --> 00:00:35,730 These types of functions can be really handy. 10 11 00:00:35,730 --> 00:00:43,800 Now, we also saw the upgraded version of the function, the strawberry flavor, where in addition to calling 11 12 00:00:43,800 --> 00:00:50,090 a function, we also were able to modify what the function did by providing an input. 12 13 00:00:50,100 --> 00:00:55,590 So for example, we could tell our robot how many bottles of milk to get, instead of it just executing 13 14 00:00:55,590 --> 00:00:57,840 the same road task every single time. 14 15 00:00:58,890 --> 00:01:05,220 Now, in this lesson, we're going to cover the third and, obviously, the most awesome type of functions, the 15 16 00:01:05,220 --> 00:01:08,640 chocolate flavor, which also happens to be my favorite. 16 17 00:01:08,790 --> 00:01:15,570 And in this third flavor, not only are we able to provide an input, namely how much money to give our 17 18 00:01:15,570 --> 00:01:23,640 robot, but we also get an output. In this case, it's a how much change the robot ends up with after buying 18 19 00:01:23,640 --> 00:01:24,810 our milk. 19 20 00:01:24,810 --> 00:01:30,360 So now we're talking about the type of functions that can actually have an output. 20 21 00:01:30,510 --> 00:01:33,730 Now, this is how we would create our function. 21 22 00:01:33,870 --> 00:01:41,100 We, again, have our func keyword, the name of our function, and we can if we want to have a set of parentheses 22 23 00:01:41,430 --> 00:01:44,940 where we provide our input and the data type of the input. 23 24 00:01:44,970 --> 00:01:53,250 Now, in order to specify that this function can produce an output, we add a return arrow here which is 24 25 00:01:53,250 --> 00:01:56,900 simply just a hyphen and a right angle bracket. 25 26 00:01:57,270 --> 00:02:05,430 And then, we add the data type that we expect to return, so the return type. And then we have a return 26 27 00:02:05,430 --> 00:02:08,510 keyword somewhere in the body of our function. 27 28 00:02:08,670 --> 00:02:15,060 And after the keyword is where we provide the value that will be the output of this function or the 28 29 00:02:15,060 --> 00:02:22,670 return value. And that value, of course, must match the return type that we specified earlier on. 29 30 00:02:22,710 --> 00:02:26,690 So when we call the function, this is what our code might look like. 30 31 00:02:27,480 --> 00:02:34,560 Unlike previously where we just had the function call, and then we just expected it to execute, well, in 31 32 00:02:34,560 --> 00:02:42,180 this case, we actually have an output. And this function call right here will actually become replaced 32 33 00:02:42,420 --> 00:02:44,630 by the output. 33 34 00:02:44,640 --> 00:02:52,110 So in this case, if we gave this function 4 as an input, 4 becomes the amount of money, 4 minus 2, 34 35 00:02:52,140 --> 00:02:59,010 the change becomes 2. The change that's returned or the output of this function is 2 which is, of course, 35 36 00:02:59,040 --> 00:03:01,440 a integer or a whole number data type. 36 37 00:03:01,830 --> 00:03:08,190 So this whole right-hand side of the equation becomes equal to 2 and that value gets assigned to our 37 38 00:03:08,190 --> 00:03:17,220 variable "change." Previously, we saw these two types of functions, a function that just has some code in 38 39 00:03:17,220 --> 00:03:18,930 it to be executed, 39 40 00:03:18,930 --> 00:03:24,980 and we called our function like so, which simply just does whatever is inside the curly braces. 40 41 00:03:25,230 --> 00:03:33,000 And we also had our functions with an input which can, of course, incorporate some piece of data into 41 42 00:03:33,030 --> 00:03:35,220 how it executes the code. 42 43 00:03:35,220 --> 00:03:41,010 So now it incorporates that and prints out the message depending on the input. 43 44 00:03:41,040 --> 00:03:45,070 Now, in our third type of function greeting3, 44 45 00:03:45,270 --> 00:03:47,840 well, now we might also have an input. 45 46 00:03:47,940 --> 00:03:54,150 Let's say it's a string data type, but we're also going to provide an output. And the output, in this case, 46 47 00:03:54,420 --> 00:04:02,280 I'm going to choose, is a boolean. Because what I want to do with my function is check to see if the input 47 48 00:04:02,280 --> 00:04:03,060 name, 48 49 00:04:03,130 --> 00:04:09,690 so the person at the door, let's imagine, has the name that matches somebody on my guest list, say, let's 49 50 00:04:09,690 --> 00:04:17,250 say, I'm only gonna allow "Angela" in, or if the name was equal to "Jack Bauer." 50 51 00:04:18,240 --> 00:04:21,490 So only Jack Bauer and Angela are allowed admittance. 51 52 00:04:21,570 --> 00:04:27,570 And in this case, if it did match, then we're going to return true which is, of course, a boolean data type, 52 53 00:04:27,840 --> 00:04:34,360 but otherwise, we're going to return false, so no entry. 53 54 00:04:34,360 --> 00:04:41,300 Now, when we call off function greeting3 and passing a name, let's say, my name, 54 55 00:04:41,500 --> 00:04:49,960 now this particular line, this part will be equal to the output. When the code runs, 55 56 00:04:49,990 --> 00:04:57,790 as you can see in the right-hand side of our Swift playground, it will be equal to whatever the output is which 56 57 00:04:57,790 --> 00:04:59,740 is, of course, true. 57 58 00:04:59,740 --> 00:05:04,270 Now, we can also capture this inside a variable. Let's say, 58 59 00:05:04,470 --> 00:05:11,980 doorShouldOpen, and we could set doorShouldOpen to the output of our greeting function. 59 60 00:05:11,980 --> 00:05:18,280 And now you can check and see that this doorShouldOpen has been assigned a boolean data type because 60 61 00:05:18,280 --> 00:05:24,180 the value that gets put in it is, of course, the output of our function greeting3. 61 62 00:05:24,370 --> 00:05:31,730 If you were to print doorsShouldOpen, then you will end up with the true value 62 63 00:05:31,870 --> 00:05:36,020 as long as this name is matching "Angela" or "Jack Bauer." 63 64 00:05:36,160 --> 00:05:41,080 But if it didn't, then we would end up with false. 64 65 00:05:41,110 --> 00:05:48,610 So this is our function; function with inputs and function with inputs and outputs. 65 66 00:05:48,610 --> 00:05:53,920 So now you're ready to tackle the assignment on Functions Part 3 66 67 00:05:53,920 --> 00:05:54,550 Part 3.