[MUSIC] Hello, in this lecture we're gonna talk about functions. Functions are very basic sort of programming construct, and exist in all languages. And this is because you want code to be reusable. That's the basic idea behind functions. Not going to go into that too much. By having a function, it allows you to write a piece of code that takes inputs and outputs and if you want to run this code in the future you can simply call the function. You don't have to copy and paste the code. And the code will run because you called the function. And in Swift, functions are very powerful, they're first class citizens. They're like they can be variables, they can do you can pass them around. But here we're just gonna talk about the basic defining a function and using and calling a function. So in this lecture we're gonna slowly build up an exploit features of functions and swift one by one and build up this do math function. A doMath function which will take in two numbers, two double values and an operator may be plus, minus, divide and multiply and then return the result. So first of all you use the func keyword to define a function, and we're call it doMath. And we're just gonna leave that right now, so doMath and it does nothing, we're gonna print. Print doing math. All right so this is the most basic function, it takes nothing in. It gives nothing back. When you call it, it does its thing and, we'll try that right now. If you call doMath, it'll print doMath. But it's printing up there because the print function's up there but it's called here. If you don't call it, you just defined it, it won't run. All right, time to spice it up a bit. We're gonna have to take in parameters, and to do so you specify them up here. We're gonna take a number called a. It's gonna be a double, and a number called b, it's gonna be a double, and an operator. And that's gonna be a string. And we're gonna make this a bit more. Nah, operators. Work with operation, not operator, because operator is key word. It got highlighted right there. And we're going to do something slightly more fancy there. We're just going to say performing, the operation on a and b. All right, so if we now call doMath it's gonna give us an error. Because we don't have all the prameters we require, which I'll. And here is how you would call this function here, that has three parameters. You would pass in the first number, say two. You'll pass in the second number with a label, and you'll pass in the operation like plus. All right. So that work, and it'll print performing and + on 2.0 and 1.0. All right. That's cool. But why is there no a here, right. So, as you can see, these are the variable names, as we can see, we're using them inside the function. Operation is used here. It is used here. But when we are calling it, we're only using b and operation. When you're calling it, these are labels. And the first one is omitted this due to Objective C for historical reasons. It's just it allows you to name your function a certain way. In this case we're doing math as in the function describes itself. But Objective C functions, and Swift functions generally read differently. And to illustrate that, generally you can write the function called perform. And the perform can take in an operation. Sorry. This is calling it. Let's define this actual perform function. That's just perform operation, and then on a, and I'll show you something else we can do later, and b. So when you read this it's more like a sentence, right? It's more, clear. And we can copy it exact same statement here, because this function takes essentially the same parameters. We switched up the order a bit. And when you call this, you can assay. It sort of reads into it. It says perform, plus, on a and b. The function name is almost the label, that's why it's not there. You can actually put the label of the first parameter inside the function. And what this allows you to do is have clean functions when there's only one variable, say performAdd. Sort of addOneTo, and then you pass in 1 without a label, and that's pretty nice. All right. Kind of went off track a little bit, about function naming. I'm going to say that this one's way better than doMath, and go off track here, and delete doMath. All right. So I said we can make this even nicer. This is perform plus ab. That doesn't read super nice. And what you can do is you can actually give labels to the parameters here. Perform operation on a and b. That's interesting that we get an error down here, because now we have to call it with a on and as labels. And see what just happened here. On is used when you're calling it but a is still the name of the variable inside the function and same with b. Now you can run this function like this, perform plus on 1.0 and 2..0. That's quite beautiful. And so this feature just lets your write nicer and more sort of expressive code. This is a lot more expressive than A and B. All right, so moving on. We still haven't done any math now. In order to do math, we have to return a value. And you do that by specifying a return value, using this arrow key. So it takes in this stuff, and then it does its thing, and it returns you a double, sorry. And you only have to put the type here, because you don't even have to name the return value. And now we get an error because we don't return anything yet. And we're just gonna return 0, for now. And note that we get 0 here cuz we called it. We still it the same way, except now it returns a value. We can assign that to result. And that's pretty cool, result is now 0 cuz even though it shouldn't be, it's 0. And so we're going to, so that is the basics of function, there's three parts to it, there's the name, there's the list of parameters and also it's return value. And when you call it, you just call it like this. All right, so let's just finish this function for the fun of it. We're gonna switch on the operation. I'm sorry, we're gonna define a variable result that's a double. We're gonna switch on the operation And if operation is a plus. Then result will be equal to A + B. If it say -. So [INAUDIBLE]- B. And if it's a multiply, result equals a times b, and last but not least, if you wanna divide, result will be equal to a divided by b. All right, and this is gonna gonna err it, cuz remember switch statements have to be exhaustive and obviously we haven't covered all the cases of operation which is a string, so it can be anything. But we can't really handle all of them, so in default case we're just gonna print bad operation. And we're going to let you know what's your bad operation. Wrong button, there we go. And we're going to return the result. What does that do? That give us an error because result isn't initialized and this is because there's a case where result is never assigned, which is this default case. So we better give it a value or in the default case we can assign a value there, and now we're going to quit up here. All right. So, now our result is 3, awesome. What if we do a minus? It's -1. Wow, this function is already doing so much for us. It's 0.5. Great, and what if we say hello? It's gonna give a zero, but it's gonna tell us that it's a bad operation. So that's pretty cool. And I hope you enjoyed this overview of functions.