Up until this point, we've looked at variables in Swift, we've looked at for statements, if statements
and loops of that kind.
What we're missing, though, is a way to organize groups of tasks that we have to run, and the way
we do that is to create a function.
Now, you've already run a function because print with open and close brackets is a function.
You know, it's a function because we have these brackets that open and close and we know it uses data
because we pass data over to it.
That is the hallmark of a good function.
Print does exactly what it says on the tin.
We don't have to give it anything explicit or extra.
We understand what it's going to do.
So let's create our own function.
You define a function with the key word func in swift in other languages.
You might have the full word function or no word for function even.
It depends on what language you have, but all languages have functions.
Now we're going to have a function called Garet.
And in order to define it, to name it, we give it the name of greed and we open and close some brackets,
we'll see what those brackets do a little later on and then we open the curly brace.
And when you press enter and playground's, it automatically adds the closing curly brace for you.
So this is the enclosed inside of here.
And what we're going to do is call.
Print hi there.
So whenever we run this function, we're going to get high there, print it out.
So how do we run it?
Well, you know how to run a print function and running a great function is just the same, except it
doesn't require any data.
If it did, the data it needed would be between those brackets.
We're going to do that, don't worry, a little later on.
So if I run, this great is going to run high there, OK?
Or if you look at the command line, it's printed out at the bottom.
So that's how we create a function and then how we run it.
Now, if I do something like this.
VA eight equals nine.
Ignore that error, in fact, to make the error go away.
We're going to do the following backslash A.
OK, that error should go away now and in fact, we'll change that to let because it's complaining about
the FA.
Can I access the A outside of the function?
No, because when I type it, it doesn't come up in the list and it says cannot find a in scope.
Scope is an important concept.
Scope refers to the area around which you can access certain things.
So inside of a function, if you declare a variable in swift, this is you can only access that variable
inside the function.
You can't access it outside the function, you can't access it pretty much anywhere else.
In some languages you have global variables or you can force global variables, which is an awful idea.
Never, ever do global variables if you can avoid it.
I will admit there are situations where it seems unavoidable.
OK, so that's just a little warning about declaration and scope, so I'm going to remove that for now
and get back onto task with functions.
Now, what if I want to greet someone by name?
Well, let me copy over the following function.
After I fix this, they get.
Paste this in, so now we have another function called greed, which is the same name as this, except
this one takes a name.
OK.
So when we call this function, we pass in the name of Grant so I can actually delete this and that
should work, although I've been having issues with it.
Yeah, this requires a label, so I'm going to undo that.
So sometimes I'm not quite sure why, but it forces you to have a label for what you're passing in,
so we have to give it the label name and say the item you're expecting for that label is Grant.
And then inside the function, we're going to say hello, Grant.
And there we will see it.
OK, so these are called arguments, the data that you pass in is called an argument and multiple are
called arguments.
What else have we got?
Oh, nice, let's go for this one.
So those are arguments and here we might want to get something back because believe it or not, this
might be bad software design.
So the way we get something back is we have great and I've just changed the name of it here.
So it doesn't conflict with this one.
And if we add a little dash and an arrow.
And we give it a type, what you're saying is when you run this function, I'm actually going to give
you something back to whoever's running the function.
So what we do in here is we compile accreting and then we return the greeting.
In the previous lesson, I said we'd get onto this and here it is.
So we are returning a string, which is greeting.
So down here I'm going to call the great function with person grant in it and then that's going to return
a sentence.
So to make this a little clearer.
Let sentence equal, great, so whatever this returns get put, gets put into the variable of sentence.
And then, of course, we can print the sentence.
And everything is as we expected.
Hello, Grant.
So we've passed in data, we've got a return from whatever that function is doing, and then we handle
the return in whatever way we need to.
Right.
What else have we got?
Now, you don't just need to pass in one function.
You can pass in zero.
You can pass in two, you can pass and 50.
So in this case, we're going to pass in a first name.
Separated by a comma and then a last name, and again, we're going to return a string.
We're going to form our greeting.
And we're going to return that greeting and then down here, we're going to print the returned value
from calling this function.
OK, so you're now really getting into the depths of actually how to code you functions are one of the
fundamental building blocks.
They basically organize your code.
Right.
And a bit of a word here about naming things.
These names are all the same.
Great.
But the arguments are different, even if they have a different name, same type, different name,
because Swift is clever enough, we can actually just name them all the same thing.
And in the end, if you type in, where are we, let's go to.
Here, if you type in Greek person, Grant Swift knows that you're looking at that because it says this
one is called name.
This one takes two arguments.
It's not that one.
It's obviously this one.
OK, so it's smart enough to figure that out so you can have functions with all different names.
And where would you have this?
Hmm, say you're making a music app and you pass in a reference to a song to play, you might have to
play functions.
One play takes a euro because you're playing from the Internet and the other play takes a low profile
reference, i.e. a file type.
The system knows which one you've passed in, so it knows which play function to activate.
OK, so that's why you'd use a practical example of that.
Not that this isn't practical.
Same same thing, really.
It knows these things.
Now, the technical word for this is called a signature, right?
Because the signature is unique.
If you sign your name, everyone knows it's you.
Functions have signatures.
In fact, many things in coding have signatures.
And signature just represents what that thing looks like.
OK, I think that's it for that.
How about some homework actually before the homework?
One more thing on naming.
This function does something, it's a doing object, therefore functions should generally have a verb
attached to them, i.e. play great work.
Turn screen off, right?
That's really specific, but your function should be well described, so whoever's using it knows what
they're looking at and they know what it's going to do.
And that person includes yourself in six months time.
When you look at this code and go, what moron wrote this?
Right.
Because that happens all the time happens to me.
You just think, why didn't I name these things properly so I can save some time at this point.
All right, let's get on to the homework.
I'd like you to create a function that takes three numbers, return those numbers, each one multiplied
by three as an array.
So you are going to return an array and I'm going to give you the first line.
Of the function, so this is the base signature of the function.
It's called multiply, we pass in an array of integers and we return an array of integers.
What that array of integers you return is all of this array of integers, each one multiplied by three.