[MUSIC] In this lecture I wanna look into functions a little bit more. I wanna look in detail about how we call functions from other functions and what that does to the memory inside of Xcode. So we're gonna put the fun back in functions in this lecture. So if you remember in the previous lecture, we described a function in this box diagram where there's a box encapsulating a bunch of code, sort of a representation abstraction of the function. There are inputs going into the function and output coming out of the function. We looked at that specifically with an example of a function called dayGreeting, which took an integer N, which was a loop, the number of times we wanted to print good morning, good afternoon, good evening. And it outputted void or it outputted nothing. It had a side effect of printing out those messages to the console. What I wanna do is I wanna extract a function from within dayGreeting now and show you how that function using the box diagram gets called from dayGreeting. So let me show you pulling that function out. We're call we're gonna create a function called Good Day. So, what we're gonna do, is we're gonna start with our code, here that has our loop in day greeting. And we're going to create a new function called goodDay, that takes as an input, a sequence of characters. And it's going to just print Good, followed by which ever characters get passed into that function. What we're gonna do then is we're gonna use that to replace those three lines good morning, good afternoon, good evening. And we're going to instead call goodDay with the suffix of a phrase that we want to repeat. And then this way we'll create a little code reuse. By replacing that printout statement by a call to goodDay. Now this isn't a super important change that you might want to make in actual code. It's to demonstrate this sequence of function calls that are being conducted from one function to another. Okay, so when we put this all together, we have the main function calling dayGreeting. DayGreeting gets called, it loops twice calling goodDay, and goodDay prints out good with whatever suffix we passed it. We do that three times. Then when we go to run the code we can see that the output of the code is exactly what we expected. Good morning, Good afternoon, Good evening are printed out a total of two times. All right, so that happened there is we created another function called goodDay, and so when we called dayGreeting, dayGreeting in turn called goodDay. GoodDay did its work by doing a side effect and returned nothing. DayGreeting then did some more work and called goodDay again. In fact, each time dayGreeting went through the loop, it called goodDay three times, and then dayGreeting quit. So if we map up this function call, actually it's starting with the main function, rather than dayGreeting. With the code's that next to it, you can see the sequence of calls that are going up this diagram from main calling dayGreeting, dayGreeting calling goodDay, three times, and then goodDay executing a print off statement. When we execute it, we see this output, which is what we expected. And it's important to notice though that when we define these functions, the order from top to bottom matters and this goes back to that idea of name space. And the fact that name spaces are ordered, these functions have names but they're only defined in the order in which they appear. So if we try and define main in the beginning of our function in the beginning of our file. And when we refer to the function dayGreeting, the compiler gets confused because it reads from top to bottom. It tries to understand what dayGreeting means, but it has no record of something called dayGreeting. So you get this warning right here saying that dayGreeting hasn't been defined. GoodDay does the same thing. And so dayGreeting defined at the bottom, and then we get an error because it's been referenced before it's been defined. So we need to declare the body, we need to declare or define these functions from top to bottom before they get used in order for us to create this internal function calling mechanism. In fact, let's have another layer to this call, in addition to what we already have with main, dayGreeting, and goodDay. Let's add another function called allDay, which is gonna take those three calls to goodDay and collapse into one. Again, this is sort of an extreme case of using functions, but it's to demonstrate this effect of stacking up functions, one on another. Another thing we're gonna do is we're gonna use Xcode's ability to extract a function automatically for us. And so what we're gonna do is after saving our code, we're going to highlight the code we want to create into a function. Xcode will create a function for us, and replace the code that we highlighted with the function call. So here's an example of us doing that. What we're gonna do is first of all we're going to highlight the code that we're interested in. We're gonna commit our code to the source code repository, essentially saving it. Give it label describing what we're doing. And then once we're done with that, we're gonna go back to our code, and we're gonna highlight those three lines that we wanna create into a new function. We're gonna go up and we're gonna select extract. And from extract, we're gonna extract a new call. And Xcode's gonna do some analysis of our code for us, and in that analysis it'll give us a chance to name the new function and we'll name it allDay. It'll give us our proposed changes and I won't bother looking at them here, so we'll just save those changes. And we can see that what it's done, what Xcode has done for us it has replaced those three lines with a call to allDay. And it has created a function, allDay, which takes nothing as input and provides nothing as output, void and void. But in the body of allDay, calls goodDay three times, with each of the suffixes that we wanted to see called. And when we run it, we can see that these three, that we see the three greetings called twice. Same way, we've just introduced another function into this layer. Okay, so this pile of functions that's called during the execution of our program has a name. It's called the stack, and it's a stack of function calls, one on another. That help us to execute code in a way that's reusable and abstract. So in summary, functions can call other functions. We saw it in this example. A function can have inputs, and a function can have an output. Functions are very important for abstracting repetitive work that computers do. We'll see a lot more from functions in the future, but that's a good start on the idea of a stack. Thanks. [MUSIC]