Welcome to another Swift Deep Dive. 1
The goal of this lesson is to really understand the Swift syntax behind functions with inputs. 2
Our current problem is that when our xylophone key is pressed, we want to pass over the title of the 3
button to our play sound function. To solve this problem, 4
we have to learn about function parameters, arguments, and data types. 5
Now, if you think back to the greeting function that we created earlier on, all it does is say "Hello!" 6
But what if we wanted it to do something slightly different every time? 7
What if, say, we wanted it to greet whoever came to our house, so it would have to say "Hello, Angela!" or 8
"Hello, Jack!" depending on who's actually at the door, 9
how can we pass the name of that person to the greeting function as an input? 10
Well, this is how we might create a Swift function that can take an input, so that way, the input can be 11
incorporated into the body of the function to do something different every time. 12
In programming lingo, the input is called a parameter and the parameter is a bit like a variable without 13
a value. 14
All we have to do to create it is to give it a name and then give it a data type. 15
So here would be the name of a parameter and then it would be a colon, and then the DataType of that 16
parameter. 17
Previously, when we were creating variables or constants, we've kind of just ignored their data type. 18
But in fact, every variable in Swift gets assigned a data type when it's created. 19
This means that the variable can only hold data of a particular type. 20
So in this case, we have a variable code Angela that's holding a Int type data. 21
So you could also create this like so. And here, after a colon, we specify the DataType of the data that's 22
going into this variable. 23
Now, how come we were able to create our variable like this without assigning the data type? 24
Well, Swift also has a capability called Type Inference which means that it's able to infer the DataType 25
based on the data that you've put into the variable. 26
So in this case, our data is a whole bunch of whole numbers, 27
right? 28
There's no decimal places and there's no text. 29
So this has assigned the type or the DataType of an Int. 30
Now, the thing about the data types are that they have to stay the same. 31
So if you create a variable that's meant to hold integers, 32
well, it can only ever hold integers in its entire lifetime. 33
And if you try to fit a different type into the variable that holds integers, 34
well, it's not going to fit. 35
It's just like those children's toys which you have to fit a particular shape through a particular hole. 36
And if you try to fit a star through a square, then it's not going to work. 37
Let's see Swift data types and type inference in action. 38
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
equal twelve. 40
Now, in this case, because we made this variable myAge, store a piece of data that is a whole number, then 41
myAge actually gets assigned the data type of int. And we can confirm this by holding down the option 42
key and clicking on the name of our variable. 43
And you can see that this is the DataType, that it managed to infer from our value right here. Remember 44
that we can change the data that's stored inside a variable by simply setting it to equal a new piece 45
of data, but we can never change its data type. 46
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
me an error and it's going to say that "Cannot assign value of type 'String' to type 'Int.' 48
So myAge is of type int "Three." 49
This text here is of type 'String.' 50
So there's a mismatch on this line right here. 51
Now, if you change the data type of the variable when it was first created, then it wouldn't give you 52
any errors. 53
It would be able to accept a data type of string. 54
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
forever only hold string data. 56
So this will now give us an error. 57
Now, instead of letting Swift infer which data type we want, we can also assign it at the point where we 58
create the variable. And to do that, we add a colon, and then we specify the data type. 59
So in this case, it would be a String and all the data types start with a capital letter. 60
So we could say it's a String or we could say it has to hold an Int. 61
Now, notice how the assigned data type will override whatever value you put in there. 62
So this line has a complete mismatch and we get an error. Because this myAge is, of course , of type Int, 63
because that's what we said it would be. 64
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
the data type 66
we specified. So now that we've understood about these data types, 67
let's go back to creating a function with an input. 68
When we create our function, we specify the data type of the input that we're expecting. And when we call 69
the function, we call the same function name. 70
And then in between the round brackets, we specify the input, or in programming, 71
it's called the argument. 72
So the parameter is the name of the input. 73
The argument is the value of the input that we actually pass over when we call or trigger the function. 74
Let's create another function called greeting2 75
and this function is going to have an input parameter in between these round brackets. 76
The input is going to be called whoToGreet and it's not going to have a value, but it does need a data 77
type. 78
So the whoToGreet which is the name of the person that this function has to incorporate is going to 79
be a String data type. And that's it. 80
So this is almost like what we did for creating a variable other than giving it a value, 81
and we're not using that var keyword. 82
Now, we're planning ahead. When this greeting2 gets triggered, 83
what do we want to happen? 84
Well, we want to somehow incorporate this 85
whoToGreet variable. 86
So let's say that we had a printed message which just says "Hello," 87
and then it says the "Hello" to the whoToGreet. 88
But, of course, in order to insert a variable into our string, we have to use our string interpolation, 89
and then we can write whoToGreet inside. 90
So now if we call this function greeting2, you can see that it automatically inserts the name of our 91
parameter whoToGreet 92
and then here we've got a little place holder to specify the value. And whatever we put in here is going 93
to be assigned to this variable and then it's going to be printed out right here. 94
So let's put in a value of "Angela." 95
Now, if I run all of my code and this line triggers, then it's going to print "Hello," 96
and then it's going to add that name Angela. 97
So that Angela got added in as the input when we called greeting2, 98
so it moves to be the value of this parameter 99
whoToGreet, 100
and then it gets used inside the body of the function. Now try calling this greeting2 function that 101
you've created with some different values and see what you get. So I could say greeting2 102
and I could say "Jack Bauer." 103
And now if we run our code, you'll see that 104
it'll write 105
"Hello Angela," 106
"Hello Jack Bauer." Our greeting function is no longer always doing the same thing anymore. 107
Instead, it's adapting to whatever input we give it and it's able to achieve different in outcomes. 108
So even though, once we've created a function, we can't really change what's going to happen with that 109
function ever again, 110
but we can still make it do different things 111
as long as we provide different inputs. Function with the input vastly improved the reuse ability of 112
our functions by providing parts that can be modified. 113
So if you think back to the analogy of our housekeeping robot, it'd be really useless if it could only 114
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
the next day, and we can use that input within the body of the function. 116
So now that you've seen how you can create functions with inputs and how you can call them later on, 117
it's time to complete the Functions 2 coding exercise and complete that assignment. 118
assignment.