 The last aspect we're going to talk about this week is this idea of something known as a function. And you can see, I've used some big fancy words here about referring to this as a control structure. And the reason why is because now that we have sort of the base foundation, we can represent numbers, letters, words inside of Python, and we can store them inside of variables. One of the things that we like to do is we like to place all of the algorithms inside of functions to make them now available in memory. Very simply, once again, if we think about the idea of that memory stick storing all of our values. So I have sort of the number five stored somewhere in memory and I have the value to stored somewhere in memory. Well, we were talking about the idea of those different algorithms that we might also want to sort of know about imperative knowledge. For example, if I wanted the area of a circle, right, that was area equals pi r squared. In essence, you can think about the five and the two, those are variables, they just store some value in the variable is the location in memory. Now a equals pi r squared, we could consider that a function where it's the same concept, I give my function some name, and it is just referencing a set of instructions that go or are stored inside of memory. So when we think about our code for a second, right, everything that I've shown you up to this point has been realistically sequential, everything happened sort of in order and there wasn't really any jumping around. Technically, yes, I can go up to a new or previous slide and change the values. But everything operates sort of in a specific order, I have some value where I make say, for example, five, and then maybe I do some calculation of five times two, and then maybe I come in and I print that value. So x equals five, y equals x times two, and then print y. Well, one of the things that you might notice here is sort of what I'm doing with that print statement. You see, if we think about it for a second, going all the way back to your math class, you might remember something like f of x. And this is the idea where you were taught, Oh, you know, I can plug in some value for x, and I will get some value for y or some value out of that x. So if, for example, x was equaling four, f of x might be something like five, where all f of x does is add one to the x. So we're realistically, when we start to build functions, we're doing a very similar approach. If I come in, and I say something like hello, or print hello, well, the same idea is coming into play. This f was just the name of my function in math, you know, we always just it's short handed to f. But that's the name of the function. Here, I'm naming the function print. So just to kind of see this side by side. And then in the same context, I'm using my parentheses to represent what the values are for my function or what my function needs to operate with. Now, again, in the same example, I was passing it something here called x. Now, it is a variable, but we also give it a another term, when we're dealing with it inside of a function, and we call this a parameter. So in this case, print takes in one parameter. Let's say, for example, I made a new function, right, and to sort of design it out. Again, just like we've seen in sort of math class, I have a function called g that takes in two parameters x and y. And all this is going to do is do x plus y. So, you know, simple addition. Now, when I would come in, and utilize my g function, let's say, for example, I create two variables. So the first variable is num one, five, and then num two, seven. So as of right now, again, these are variables stored in memory, if we're thinking about them from a roll perspective, these could be classified as constants. In the sense that I don't want to change num one or num two. But then when I'm utilizing it inside of my g, I would do a few different magical terms. But effectively, I would call this passing num one, and num two. So we're passing those values to my g parameters. So in essence, num one is going to go where that x is. And then num two is going to go where that y is. And again, if we know the value of num one, and we know the value of num two, and we know what the algorithm is for g, then we would know that this is going to give me the value, in this case of five plus seven, or 12. So how do we build this out? How do we take that and sort of design it into Python? So there's a number of different sort of parts to this. The first is one of the things that we need to do is we need to first establish a keyword called death. Death's sole purpose is to tell Python, Hey, I'm about to name a function. And that's exactly what I would do next. In this case, I'm saying, Hey, I want to make a function. And I'm going to call this function function name. Then with my param with the quotations, I specify the number of parameters, and the temporary names that I'm going to give them. So in this case, I might call something a param one. And if I know that this function needs to have two parameters, then I would give the second name as well. We're then followed up by a nice little simple colon. Again, this is really just identifying, Hey, I'm about to tell you the algorithm of this function. And then I have a tabbed in tabbed in. I've added in white space for all of the different commands that I'm going to be putting inside of this algorithm. And this can be any number of statements that you want. It's really just at that point, you're building out the actual algorithm. Then finally, you have something known as the and I'll change the color for this. Then you have something known as your return statement. So again, if we look at that where you that visualization here, this x plus y, that is what the value I want to return, I want this to be the byproduct of my algorithm. So just to sort of see this in action. Let's say I wanted to take that area of circle was pi times r times r. So pi r squared. Okay, well, you know, I have to build out this function. So how do I like to go about doing this to start? My typical approach to this is to ignore working with the def to start, right? The first thing I would like to do is take whatever parameters that I know I'm going to be working with. Again, these are very similar to our constants. When we think about their role, I don't plan on changing radius, but I know I'm going to need radius. Okay, and I'll just give it some arbitrary value for right now. Five, again, this is just me working off of my calculations. Now, I am also going to give something called pi and I am capitalizing this this is sort of a more of a accepted notion, where if you're dealing with constants, you want to keep them capitalized to symbolize you're not going to change it. In this case, I'm going to do that. But for our sake, I'll do 3.14. I could go further but for demonstration purposes, I'm just going to do 3.14. And then I need to do the actual algorithm, the mathematical calculation. So area is going to equal my pi times radius times radius. Okay, so I've built out my my algorithm and I'm now just going to test it out. This is sort of me working through the process before I build out the function. So again, print my area. And so I get, in this case, 78.5. So pi times radius times radius 3.14 times 25 effectively. Now, I want to convert this into a function. So again, the first thing I do is I start with def. I'm telling Python I plan on making a function that I need to store in memory. And I'll give it a name area circle. Okay, I'm just giving it a name that's descriptive. It's the area of a circle. Then I need to establish what my parameters are. Realistically speaking, I don't need to give pi here. The reason why is because pi is sort of well established. Everyone knows what pi is. I never am going to change pi. That radius would change. So I'm going to come in. And I'm going to name that variable radius. Okay, I could name it or I could name it whatever I want. But again, I'm trying to use very descriptive variable names. And then the colon again, this colon is just telling Python that we're ready to place that place statements for the actual algorithm in to our code. In this case, well, all right, I need to build a pie. I don't need to but I like to this is more of a design choice for my sake. So again, pi is going to equal 3.14. So I'm just building that out. It'll always be the same value regardless of what radius is. And then area. Again, we're just doing pi times radius times radius. Now I'm going to go ahead and just compile that or interpret it loaded into memory. And there we go. And now I can use it. So in this case, area circle five, and I should see the exact same thing. I didn't see anything. Maybe I need the print statement to make this happen. No, in fact, I get a none. So something is actually wrong with my function. Well, that is because I'm missing that last little piece that last nugget of information, my return statement, the calculation is happening, right? The calculation is actually occurring area is becoming 78.5. But I have no way of saying to give that back afterwards. And so I'm going to just come back up and add in a return statement return area, taking that value, reloading it into memory because I have to I've just changed it. And now if I do that same thing, I still run area circle five, what I should see because it is what I've seen in the past. Ah, what do you know? I am getting the 78.5. Now the benefit of a function is that I don't have to copy and paste and run this command over and over again, it's stored once in memory. And now I can make reference to it as many times as I want. So I could come in, do some fancy copying and pasting of five 152535. And just for my sake, I am going to add in the value beside each one of these 152535. So where this is going on is now what I should see is for each call of the function, every time I invoke my phone, I'm using big words, but each time I basically execute or make the function happen, I'm also just going to say what that value was that I happen to do it with. And that's just more from a visual standpoint that I can see. I'm not just seeing 78.5, but I'll see that's from my five version. And so that's exactly what I see. The area of a circle with a radius of five is 78.5. The area of a circle with a radius of 15 is 706.5, et cetera, et cetera. And so you can continue to do this over and over and over again. So just to jump through those are those commands. Again, just the big focal points are those that move it here. When we're referring to them, this is more, you know, splitting hairs, but I do at least want to make sure you're aware that way when the terms come up, they don't confuse you so much. But when we are referring to a variable as it's being used inside of the function, we call it a parameter. When we are using, we're putting a value into that function, or we're executing that function, making that function happen, we refer to it as an argument. And the weird annoying thing is those two will flip, you know, read a blog post on functions and someone's going to say that this is arguments and this is parameters. The idea is they're the same term, they're referring to the same thing in essence. So it is something to kind of be mindful of. So just to see another example of this, let's say for example, I was taking that idea of calculating out something like a total and I wanted to format money with it. Well, in this case, I would have two parameters, subtotal and tax. And the idea is again, I'm taking these parameters and I'm doing something with them. I don't want them to change the subtotal will always be in this case, 4.9 and the sales tax will always be 0.7 or 0.07. So again, those are established. And so now I make my total. And then I'm just doing some fancier stuff here. This is more along the lines of me. I'll describe this fancy decimal formatting. The entire idea there is because we're dealing with decimals, there might be thousands that we're working off of. But the big thing that I want to sort of show off here is the fact that Python happens to have its own functions to work off of. So let's take that same example and kind of build off of that. So item price equals 4.9 sales tax equals 0.07. Well, you might remember that if I just did the item price times the sales tax, right, we got this crazy variable or sorry, this crazy value that extends outward. And because how floating points operate inside of Python, it's not going to be a beautiful round number. It will again get into the craziness of this later on. But the idea is, oh, well, you know, I'm not going to charge someone one thousandth of a cent when I'm trying to do, you know, calculations on sales tax and everything, I would round it up. And there are ways you can do rounding. But if I wanted to format this out in some way, right? Well, okay, I'm going to build those I've established them again, I'm going to now def format. What am I calling this format money with my subtotal and my tax. And I am going to cheat a little bit and over here on the other side, I'm going to move. So to take that same code that we see in the slides, total is going to equal subtotal plus subtotal times tax. Again, the entire idea here is just my 499 plus 499 times 7% tax will give us our actual total because taxes will operate. Now, the big thing that I want to show off here, and in fact, I'm going to once again cheat and get rid of the function because again, I want to make sure what I'm doing is correct. The first thing I'm going to do is I'm going to convert my total into an integer just to see this in action for a second total times 100. And just so we can see what's going on the entire time, I'm going to print a total after that first total. And I'm going to print another total after that second total. So I run this So again, what we're kind of seeing here is total by itself when we did the calculation of 4.9 plus 7% tax, we're getting roughly speaking 5.3393 bunch of zeros one. Again, I'm trying to format this so it looks like a respectable currency amount, we're going to ignore rounding up and rounding down, we're just going to focus on trying to get it to be the amount of dollars, a decimal point, and then two cents places. So the first thing I did was I took that and I multiplied this value by I took this decimal point floating value, multiplied it by 100. Okay, that was here. But Python again has its own functions that it exists in its own little background. Technically speaking, guess what, print is a function that does a thing, you know, Python built that. So very similar to our idea of depth, there is a depth int somewhere in Python. And what int is doing is saying, give me a floating point number, and I am going to convert it into a whole number. So give me this five, well, it'll be 533.93001 it give me that number. And let me convert it into a whole number. So let me truncate out the total or sorry the decimal place. Now, just what I was going to do in the slides, I'm going to take that value, that whole number, and I'm dividing it by 100. So I take that total. And I divide it by 100. And just to see that again, I'm printing it out again. And so what we should see is 5.33. Awesome, beautiful, fantastic. The last little bit that we need to worry about is, again, we're just trying to make this look like a dollar a price. So I would come in. And I'm going to give it a new word. I typically like to use the word line when I'm building something I want to print instead of a calculation, you know, that part can change. But again, we're dealing with a string character. That's dollar sign. So the entire idea here is that dollar sign, I can't just say dollar sign Python doesn't know what that is. I can't. I can't come in and say line equals dollar sign plus total. It freaks out about that. Because again, it doesn't know what the dollar sign is. But it does know. Oh, I want to come on. There we go. I want to put the string character of the dollar sign next to my 5.33. Ah, but as you can see, it's still complaining. It's another error going on here. This time, and this is where we can do a little bit of debugging. The issue is, I cannot do a string concatenation to a number. If you again, think about it string, this is a character, it doesn't handle, you know, it's just saying this is a symbol that I want. And this is 5.33, numerically, Python looks at these and goes, Well, this is a string and this is number. Uh, I can't do that. I can't, I can't, it's trying to do 1.8 divided by cat again. It doesn't, you can't. So what happens is just like we had a function in Python to convert something into a whole number, we also have something in Python to convert a number into a string str. So I can come in and just like we've done in the past, I'm invoking another function. I'm going into Python and saying, Hey, there is a function out there called str that you have in your programming. I want to do it. And what I know is going to be given back to me is, in fact, quotes on my string. And just to see this in action, str total, you can see it output that out with those little quotes on there, because again, I've converted my numerical value of 5.33 into a string representation, the characters 5.33. Now if I did something like line equals plus or dollar sign plus str print line, and I get my format. Okay, now that I've taken all of this, the entire idea is how do I convert this into a function? Same kind of process that I've done in the past, I'm going to do some cleaning up of sort of this cell so that it looks like a function. The first thing is, I'm going to get rid of all those print statements. The print statements are great for debugging and working through the code. But you know, when you're done, start to get rid of them, comment them out, things of that nature. The next is that I need to build the depth. So again, I was going to call this format money. And it is expecting two parameters, my subtotal and my tax. So subtotal, colon. Then I'm just going to do some quick indentation. One thing that is really great, and this is more of a spider or sorry, a Jupiter thing or a codering thing. I'm going to just highlight all three lines, and then hit the tap key. Oh, that's so lovely, isn't it? It's going to do the indentation for me. I don't need to worry about it. The last little thing, just as we've said in the past, I need to return my variable. In this case, I want to return the string representation of one point or dollar sign, whatever my sales total is. Now that it's loaded into memory, I can give it whatever I want. So if I came in, I could say format money, 499, 0.07, it's going to show again the 5.33 proves that we've done what our thing is. But I can now pass it different parameters. This is where we can get a little funky. There is a subtotal here, and I can in fact use it. And it will do the same thing. It will freak out when I, sorry, it won't freak out when I do subtotal tax here. But I can also give different names to my functions, or I can use different variable names as well. So again, this is where that item price, 799, sales tax, 0.08, and I can do the same thing. So format money, item price, and sales tax. And what I should see is again, the idea of this variable and this sales tax are going to act as the arguments for subtotal and tax. So it's just going to say, oh well, you were calling this 799 item price. For the purposes of handling our algorithm, we're going to refer to that item price and sales tax as subtotal and tax. So again, we take it, we run it, and we can see in this case, it's 8.2, or 6.2. So just walking through, this is the entire process of sort of showing that off as well. And walking through it again, so it then passes that gets that calculation, spits that out, makes that change 5.33. Now, the last little bit is this idea of nested function calls. So just as we were kind of showing off in the code, right? Here, I was showing you something like formatting tax, and I was showing you I can make function calls inside of function calls and things of that nature. But the big thing that I was getting at here is that something like this is perfectly legal a a a six times eight, perfectly allowed. And in fact, all you're doing is you're treating it like PIMDAS. So again, six times eight, that's the innermost parentheses. So we do the calculation. That is, I believe 48. So then we take that one step further. And we do, well, whatever a on that 48 is doing. And that's going to give us a calculation. And then we give the next whatever that calculation is, and we give that the next whatever that calculation is. To see this sort of an example, let's build an a that is expecting some number. And for our sake, let's just say whatever that number is, we're going to multiply it by two. So return number times two. Okay, well, if we think about that again, six times eight is 48. So if I then came in and said a six times eight, well, 48 times two 96. If I took that and placed it inside of a. So now a calling a on a calling on 6.8 in 42 or 48, that gets evaluated into 96, which then is just going to get evaluated into 192. And you can already see just to reidify this one more time a to a to a 8.6 384. And I can continue on or I could add in new variables or functions to this entire equation. But the entire idea is again, it's just it's no different than if you're working with a, what's the word, no different than if you're working with PIMDAS and mathematical operations, always focus in on evaluating out the inner most value first.