Hey, guys. 1
Welcome to another Swift Deep Dive. 2
And in this Deep Dive, we're going to talk about functions that can have outputs. 3
So far, we've already seen two types of functions. 4
The most basic or the vanilla version if you will is simply where we package a bunch of code together 5
and we execute them 6
line by line in the same order. 7
So that saves us from typing lots of code repeatedly, and especially when we have the functionality that 8
needs to be executed multiple times. 9
These types of functions can be really handy. 10
Now, we also saw the upgraded version of the function, the strawberry flavor, where in addition to calling 11
a function, we also were able to modify what the function did by providing an input. 12
So for example, we could tell our robot how many bottles of milk to get, instead of it just executing 13
the same road task every single time. 14
Now, in this lesson, we're going to cover the third and, obviously, the most awesome type of functions, the 15
chocolate flavor, which also happens to be my favorite. 16
And in this third flavor, not only are we able to provide an input, namely how much money to give our 17
robot, but we also get an output. In this case, it's a how much change the robot ends up with after buying 18
our milk. 19
So now we're talking about the type of functions that can actually have an output. 20
Now, this is how we would create our function. 21
We, again, have our func keyword, the name of our function, and we can if we want to have a set of parentheses 22
where we provide our input and the data type of the input. 23
Now, in order to specify that this function can produce an output, we add a return arrow here which is 24
simply just a hyphen and a right angle bracket. 25
And then, we add the data type that we expect to return, so the return type. And then we have a return 26
keyword somewhere in the body of our function. 27
And after the keyword is where we provide the value that will be the output of this function or the 28
return value. And that value, of course, must match the return type that we specified earlier on. 29
So when we call the function, this is what our code might look like. 30
Unlike previously where we just had the function call, and then we just expected it to execute, well, in 31
this case, we actually have an output. And this function call right here will actually become replaced 32
by the output. 33
So in this case, if we gave this function 4 as an input, 4 becomes the amount of money, 4 minus 2, 34
the change becomes 2. The change that's returned or the output of this function is 2 which is, of course, 35
a integer or a whole number data type. 36
So this whole right-hand side of the equation becomes equal to 2 and that value gets assigned to our 37
variable "change." Previously, we saw these two types of functions, a function that just has some code in 38
it to be executed, 39
and we called our function like so, which simply just does whatever is inside the curly braces. 40
And we also had our functions with an input which can, of course, incorporate some piece of data into 41
how it executes the code. 42
So now it incorporates that and prints out the message depending on the input. 43
Now, in our third type of function greeting3, 44
well, now we might also have an input. 45
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
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
name, 48
so the person at the door, let's imagine, has the name that matches somebody on my guest list, say, let's 49
say, I'm only gonna allow "Angela" in, or if the name was equal to "Jack Bauer." 50
So only Jack Bauer and Angela are allowed admittance. 51
And in this case, if it did match, then we're going to return true which is, of course, a boolean data type, 52
but otherwise, we're going to return false, so no entry. 53
Now, when we call off function greeting3 and passing a name, let's say, my name, 54
now this particular line, this part will be equal to the output. When the code runs, 55
as you can see in the right-hand side of our Swift playground, it will be equal to whatever the output is which 56
is, of course, true. 57
Now, we can also capture this inside a variable. Let's say, 58
doorShouldOpen, and we could set doorShouldOpen to the output of our greeting function. 59
And now you can check and see that this doorShouldOpen has been assigned a boolean data type because 60
the value that gets put in it is, of course, the output of our function greeting3. 61
If you were to print doorsShouldOpen, then you will end up with the true value 62
as long as this name is matching "Angela" or "Jack Bauer." 63
But if it didn't, then we would end up with false. 64
So this is our function; function with inputs and function with inputs and outputs. 65
So now you're ready to tackle the assignment on Functions Part 3 66
Part 3.