 Hey guys, I'm going to be going over the functions lab one, question three. So for this question, we are given the purpose of what a function should do, and we are supposed to write that function. So we have the definition of a function, which is where we put the actual code for what the function needs to do, and where we do all the declaration for the function. And then we have the call, which we can do up here in setup. So for 3.1, we are meant to write a function called smaller, and it's going to take two integer values, and it's going to return the smaller of those two values. So we know that the name of it is going to be smaller. We know that it's going to have two inputs, and both of those inputs are integers. So we just have int num1 and int num2, and then we know our return type is going to be an integer, because we're just going to be returning one of these numbers. Alright, so for this one, it's pretty simple in terms of how we're actually going to solve it. We can just use an if statement for it. So we know that if num1 is less than num2, then we are going to want to return num1 anything else. So if num2 is less than num1, or if num1 and num2 are equal to each other, then we want to return num2. So now we can test this in our setup. And we can create a new integer called result, and we can call our smaller function, and we'll just put in two random numbers, we'll say like three and eight. And then we can print result to see what happens. So let's just test this out, and we get three, because we know three is smaller than eight. So the second part of this question is asking us to write another function called smallest that takes three numbers. So it's going to be pretty similar to smaller. We know that it's going to return an integer. We're going to call it smallest, and it's going to take three inputs, and all those inputs are going to be integers. So one thing to notice here is how these variable names are same as these variable names, but it's really important to remember that this num1 and this num2 can only be accessed within this function. And this num1 and this num2 of smallest can only be accessed within smallest. So smallest can't access num1 and num2 of smaller. Same with smaller. It can't access num1 and num2 of smallest. So even though they have the same variable name, it's fine that we do this because these are local variables for these functions. So for here, we can actually use smaller to write smallest. So instead of us having to rewrite all this code and say if num1 less than num2 and num1 less than num3 and all of these different if statements, we can just call smaller. We can create a new variable temp for temporary, and we'll call smaller. And we'll just start with putting num1 and num2 in here. And then temp is going to be equal to whichever number is smaller that, and then we can create a new result, and that's going to take our value of temp and put it into smaller with num3, and then it's going to return whatever's smaller for those. All right, so what's happening here is we have this num1 and num2 that gets put into smaller. So now these two numbers will be put into this function as this function's two inputs. The smaller number will be returned and put into the variable temp, and then we're going to call smaller again with temp and with num3. So you can actually shorten this a bit. And if you feel comfortable using nested function calls, we can actually just say return and then we can call smaller, and then we'll just take what we put in here. So smaller will run with our num1 and num2, and then we're going to run smaller again with num3. So now we can test this out, and we'll just call this result2. So we'll call this result1 and result2, and then for this we'll call smallest of, we'll say 4, 7, and 18, and then we'll just print that result. So now we can check this. Oh, I see where our issue is. We need to put them on two different lines. There we go. So now we get the smallest 3 and the smallest number 4. So now let's move on to 3.2, and we want to write a function that when passed two integers returns the average of the two. So let's just get rid of all this now and make our code a bit cleaner. All right, so we know that we're going to need a new function. We can just go ahead and call it average. We know it's going to take two inputs, the column again num1 and num2, and for the return type it's going to be a bit different this time, because a lot of times when you do the average, you can assume that you're probably going to get a decimal number. So for this we'll return a float. So even though we're inserting two integers, we're going to get a float return just because of the calculation that we're doing. So we know that to calculate the average, we're going to need to do a plus b, and we're going to need to divide that by two. We'll just put this in parentheses because we want the a plus b done first, and then we know whatever this is, we're going to need it as a float value, rather than an integer, because at the moment it's going to be giving us back an integer, so we want to specify that we want a float back. I put my variable nums in wrong, we've got num1 and num2, all right, what's wrong with it now? There we go. All right, so now this is going to return to us num1 plus num2, and it's going to turn that into a float, and then it's going to divide that by two. So now we can check this and make sure this works. We can call average, and we'll just print whatever value is giving back to us. And we will put in, let's say, five and eight. Now we'll run this, and we get 6.5, which I assume is correct. All right, now we'll move on to 3.3, let's get rid of all this, make our code a bit cleaner. All right, so now we want to write a function that when passed a floating point value returns the floor of that number. So if you're not sure what the floor of a number is, it's the highest integer lower than or equal to the floating point value. So if we have 4.2, the floor of that number is 4. If we have 5.8, the floor is 5. And if we have a number 6, the floor of that is 6. So for this one, we know that we're going to call it floor, and we know that it's only going to take one value and one input, and we know that this is probably going to be a float. So now we essentially just want to turn this float into an integer, because the way processing works is that it already calculates if it has a float and it needs to turn it into an integer, it's going to give us the floor of that number. So we don't really have to do anything too, too special, we just have to turn this float into an integer. So we'll just create a new integer, we'll call it temp, and then we'll just put in parentheses int, and we'll insert number 1. And then from here, we can just return temp. So we are missing our return type, so we know that the return type is going to be an integer, because that's what floor is doing, is that it's returning an integer. All right, let's see what's going on. Let's try just changing this name. Does that do anything? Nope, so we've got floor float A. Not quite sure what this means. Okay, so we just had to change the name of the function. So I guess floor is some sort of predefined processing function. So maybe I'll look that up later and look more into that. But if you're getting issues with that, just try changing around maybe some of your variable names or some of your function names, because there are certain things such as like mouse x key pressed that processing already uses. So we can't use them, or else processing is going to get confused. So now we'll just go ahead with this. We'll say print, and then we'll call our function, and we'll put in, we'll say 7.8. So we know that when we run this, we should get the integer 7, and we do. All right, so hopefully this helped guys, and stick around for question 4.