[MUSIC] I'd like to introduce the idea of functions in Objective C now. Functions are something that you might be familiar with if you've taken a math class, particularly an algebra class. In a lot of ways, functions in code are a lot like math functions. If you remember the definition of a function from math class, you might remember something like this, where y = f(x). This is a mathematical notation for a function and it has an analog to the way things work in code. Some of the features of a mathematical function are that it has a name. In this case, the name of our function is f. Our code also has some inputs. Input into this function is x. We're gonna give x to the function f, and it's gonna produce something. And what it's going to produce is going to be the output y. Functions also have a definition. So in this case, the function f(x) is defined by a body which says 2x- 1. This an example of a possible definition of f. It's also possible for functions to have more than one input variable. For example, this function has three input variables, x, a, and b. And the definition of the function includes all of those variables in order to produce y. In this case, it's the equation of a line, a times x, plus b. Those multiple inputs are necessary in order to compute the output. So in a mathematical sense, we know that a function has some properties. It has a name, it has 0 or more inputs, it has 1 output, and it has a definition. Well, in code it's similar. Code functions also have a name. We've seen names already when we have defined variables. For example, when we define int A = 0, we know that there's a variable called A and that A is that variable's name. Int is its type. Functions also have a name like that. We know that mathematical functions have zero or more inputs. Well, inputs to the functions, you can have zero or more inputs to functions as well, code functions. And those inputs are gonna be variables, like A. Functions also output something. It can have zero or one output. Those outputs have a type. Then finally, functions also have a definition. And that definition is defined by lines of code that are within it. So let's look at one that we've already seen. We've already seen in previous examples the function main. We haven't really looked at it in detail so let's break it down into the different components. So for starters, the main function has a name, and the name of the main function is main, and you can see it here. That's where, that's the place in the syntax where the name of a function goes. Functions also, as we said, have a body. And the body is the code that defines it. And in this function it's the loop with the three printf statements. Functions have an output. This main function, and all main functions in C, Objective C, have a type of int, and that's located here. It's before the name of the function. And then the value that gets returned is output, not just the type, is defined by a return statement. In this case we're turning zero and saying zero is of type int. So the output is broken into two places. Our inputs, in this case, are called arg c and arg v. Those are the names of two variables that are coming into our function. So main is a function and has all these different components, very similar to the mathematical functions that we've looked at so far. To make that connection we can put the mathematical formula next to the code. You can draw a line and show you that in a mathematical function, the name of the function is f. In the code that name is main. Here is where those two things match. Functions also have input. So here's an input and here's another input mapping a and b to argc and argv. Functions have an output. Now in code, the output is split into two types. One part is where you define the type, that's next to the name. And the other place is where you define the value that's returned. And that's next to a return statement somewhere within the body of the function. Another way that you can think about a function, is to think about it maybe in a box and line diagram. So you can think about it as a flow of data going through a box. And that box is the function, and within that function is code. The inputs flow into the box, the code does something to the input, and that data flows out as a result. In our case, the inputs were, in the case of the main function, the inputs were argc and argv. The code statements were the printf statements. The output was a type int and the name of the function was main. So, you can also think of that in this way, in a box and line diagram. So, let's take a second and let's write our own. What we're gonna do is we're gonna write a function whose name is gonna be dayGreeting. It's gonna take one input and it's gonna have zero outputs. We're gonna do this to test our understanding of the syntax of the function. So here's the function that we've been working with. It's the main function. It has a loop that writes three lines Good morning, Good afternoon and Good evening in a row with the number of times indicated by the loop. What we're gonna do is we're gonna write a definition of a new function called dayGreeting. We're gonna give it a return type of void that means there are no outputs. And as input we're going to have one variable called loops which is a type int. Within the body of our function we're going to take the code that was in main and we're going to move it up into this new function that we're gonna create. We're gonna create a loop that rather than going from 0 to a hard coded number like 2, we're gonna go until i is less than, until i no longer less than the variable loops. That was the input to the function. But otherwise this code is going to be exactly the same. Then, when it comes time to call this function, we're gonna call it with the number of times we want the greetings to be written out using the printf statements. And we'll do that by replacing the original lines of code with a call to the function. You can see that blue marker there. I accidentally indicated a break point. We'll talk about that in future videos about break points. In this case, we're just going to get rid of it here. So now when we run this function, the main function's going to get executed. Day greeting will get called. The number 2 will be passed to the function we just created and that loop will get executed two times, writing six lines of code. When you execute it you can see that's exactly what happens. So, what we've done here is we've defined our own function that has a similar format to the main function. Only it's using terminology or it's using sort of variables and types that we're more familiar with. So, why would we want to do this? Why would we write functions that encapsulate several lines of code in one logical unit? Well our big reason is reusability. There are often times tasks that you want the computer to do over and over and over again. Maybe with small changes that can be accommodated by an input variable, like the number of loops in our example. And rather than writing that same code over and over again, and possibly introducing errors, we can write that code once and anytime we wanna use that functionality we'll call the function. As a side note, this also improves our abstraction. We don't have to think about the details of writing those three lines each time we want to call day greeting, we could just abstractly think of making the day greeting call, function call, and not think about the details, so that we can use that as a building block for bigger and bigger projects that we might want to work with. There are a couple side notes that I need to bring your attention to. When I describe the function as this box, it's kind of like it was a black box where we have these inputs going in, code gets executed and output comes out. Well, in reality, the code that gets executed within these functions can sometimes have something called side-effects. Without side-effects, you would assume that the only thing that this function is doing is writing that output. Well side-effects mean that that function might do something else in the world, other than just return a value to the output. We've already seen that. For example a side-effect of the daydreaming code was that it wrote out good morning, good afternoon, good evening to the console. That's a side-effect of that function that didn't just have to do with the value that it returned. The value in our case was nothing, it was void. So sometimes a function changes things besides just its output. That's called side-effects and this if very common, but it's kind of a little bit different than maybe that diagram suggests about it. Secondly, sometimes it's useful to write down the first line of a function describing its inputs and its output and its name. Without declaring the body of the code immediately. This is the difference between a definition and a declaration. Sometimes you need to just name the function without providing the code for it and this is called a declaration. This is where you don't describe what's going on inside and this isn't very helpful for your code at the end, but it's very helpful for the compiler. Especially when function A calls function B, and function B calls function A. Because you have a circular condition here you have to declare one of those functions before you describe the code for it. So you may run into cases where you see functions that are declared but don't have a body defined in them and that's just called a declaration. It says that here's a placeholder. In the future I'm going to have to put in the actual body of that function before I can use it. So in summary, a function is a way to encapsulate some common code. A function can have multiple inputs, and a function can have zero or one output. Functions are really important for abstracting repetitive work that computers do. In the next lecture, what I'm gonna bring up is some more information about functions and some of the ways that calling functions repeatedly can impact our computing environment. Thanks. [MUSIC]