Welcome to another lesson on Swift Deep Dive. In today's lesson, 1
we're going to talk more about functions. And we've already seen functions in action, so we know that 2
there are two stages to any function. 3
First, the part where we create the function, where we have the "func" keywords and we specify the name 4
of the function, and then we describe what the function should do. 5
And then there's a second part where some point down the line where we call the function and the computer 6
will look to see where our function was defined, and do all of the stuff that we said it should. 7
So what is the problem 8
that a function actually solves? 9
Well, let's say that you had a little robot that you bought and you need it to fetch some milk from the 10
store. So you would have to tell it a whole bunch of instructions, say, you had to do it via print statements, 11
you would have to tell it to leave the house, and then move right two blocks, move up four blocks, move 12
right two blocks again, and then buy the milk, and then all the way back with all the instructions in 13
order to get your milk. 14
So if you had to do this day after day, then your fingers would hurt from typing literally. 15
So how can we make this better? 16
Well, what if we could take all of these instructions and packaged them together into a function. 17
And this is essentially what our functions do. It packages a whole bunch of instructions into a block 18
of code which can be triggered simply by calling the function name. 19
So now we can identify all of these instructions by a simple and descriptive name. 20
So the next time you want your robot to buy you some milk, 21
you can simply just call getMilk and it will be able to look up what it needs to do through the 22
getMilk function, and then be able to perform that task much shorter and much less typing. 23
So here are the two parts of the function, the part where we create the function, it looks like this, and 24
the part where we call the function, it looks like this, where we simply specify the name of the function. 25
So, in this case, it's called getMilk, and then we add a pair of parentheses which are empty. And a little 26
bit later on, when we look at more complex functions, we'll learn what we can put inside these parentheses. 27
But if we simply just want to trigger a block of code like this, then we just need to call our function 28
like this. 29
Now, if you open up your playground again or simply create a new playground if you can't find the other 30
one, then delete everything that's currently inside 31
to start with a clean slate and we're going to create a function of our own. 32
We're going to use the func keyword to specify that we're creating a new function, and then we're going 33
to give that function a name, 34
so we're going to call it greeting. And then we're going to add a set of parentheses and then a set of 35
curly braces if we break this line down, 36
This is the keyword that creates a new function. 37
This is the name that we've given the function. The round of braces or the parentheses is where we can 38
put some potential inputs 39
and the curly braces is where the body of the function will go. 40
So when this function is triggered, it's going to look inside these curly braces to see what code needs 41
to get executed. 42
Now, more commonly, you'll see a function formatted like this. 43
And in fact, every time you create a new function, as soon as you open up the open curly brace, if you 44
just hit enter, Xcode will automatically insert the closing brace for you in the right position. 45
And this makes it much easier to avoid these missing curly braces and make your code a lot easier to 46
read. 47
Let's say that in our function when it gets triggered, it's going to print "hello" to the debug console. 48
So if we go here click on it and hold and change to manually run, I can manually select which lines of 49
code I want to run until. 50
So if I click on, say, line six or line seven, and it's going to run all of the lines above. 51
Now, you might expect something to happen, but nothing happened. 52
Why? 53
There's nothing wrong with your computer. 54
It's actually because it's not enough to just create a function. 55
You also have to trigger it or execute it. 56
And in programming, we call that calling the function. 57
So on line six or seven, we can call the function by simply tapping in into its name. And you can see that 58
as soon as I do that, we get a little "f" symbol here, "f," a function show up and it shows us that "Hey, did 59
you mean to call the greeting function?" 60
And, yeah, I did. 61
So I'm gonna hit enter and it automatically inserts the rest of the code for me. 62
So now if we try to run our code and including the line where we called our function, well, then you'll 63
see it actually trigger all the code in here and the word "hello" actually gets printed. 64
Remember, the funky word is used to create a set of instructions and package it with a name, and the actual 65
name plus the parentheses is where you actually activate or call that function. Here's a challenge. 66
Change the code so that you get the word "hello" 67
printed four times. 68
Now, there's a few ways of doing this. 69
And as long as you can get the words "hello" printrf down here four times, then you can consider yourself 70
successful. 71
Now, 72
one way of doing this is to simply copy our print statement and have it execute four times when we trigger 73
the greeting function, that will get us our "hello" 74
printed four times. 75
But the alternative is we could have also just called our function four times, right? 76
So if I press play, you can see that this line goes to find greeting, prints "hello." 77
second line print "hello," third line print "hello," and so on. We get the same effect. Now, the last thing 78
I want to talk about before we move on from functions is this idea of scope. Scope is really important 79
because it describes when a function can be accessed or can be used. Just as if I have an apple tree 80
in my garden, then only myself or members of my family can actually pick apples from the tree. 81
Whereas if that apple tree was in the neighborhood, well, then everybody, all my neighbors, everybody on the 82
street can pick from that apple tree. 83
So here I've got two functions, one called greeting1 and the other called greeting2, and they do 84
something slightly different. 85
This one prints "hello" and this one prints "hey." Now, inside each of these curly braces, it's a block of code. 86
So you can view these as the walls of a house. 87
So that means that if I was to create a variable, say, inside greeting1, let's call it myName and we'll 88
set it to equal "Angela," 89
I can actually only use this variable myName inside greeting1. 90
So let's try and print myName. 91
That is perfectly acceptable. But if I try to do this inside greeting2 and I try to run it, then there's 92
a problem here because this myName is an unresolved identifier. 93
It doesn't know what it is because it was created within the four walls of greeting1. 94
So it's only available for use inside these curly braces where it was created. 95
Similarly, if I had created my greeting2 inside the boundaries of my greeting1 function, so greeting2 96
is now nested inside greeting1, 97
well, now I can't actually call greeting2 outside of this boundary. 98
So this doesn't work and it'll give me an error. 99
And the only way I can trigger it is by moving it inside of the block where it was created. 100
So namely right here. 101
So these curly braces matter a huge deal. 102
And that's why when you're coding, especially when you're creating functions, it's really important to 103
keep them indented in such a way that you can always follow the closing brace vertically up to the line 104
where the opening brace is located. 105
There's a couple of tricks here. 106
You can double click on the closing brace and it will highlight the entire block for you. 107
Alternatively, you can find a closing brace and hit the left and right key on it, and it will flash the 108
matching opening brace just to show you where the boundaries lie. 109
If you have your indentation really off and you're a bit messy, it might help to hit command A to select 110
everything, and then go to Editor, Structure, and click on Re-indent 111
for these closing braces to be placed in the position where they are supposed to. 112
And as you get used to this way of indenting closing braces, at some point, your eye will be able to 113
see easily that this is a block of code 114
and this is another block of code. 115
The best way of making sure that your indentation is correct is whenever you find yourself creating 116
a opening brace, 117
simply hit enter because Xcode will then automatically insert the matching closing brace. In that way, 118
you'll always have the proper indentation. 119
So that's all on the most basic type of functions. 120
The next thing to do is: complete the Function 1 assignment.