 Hey guys, I'm going to be going over the Functions Lab 2, and this is Question 2. So for this one, we're given a picture and we are meant to try and recreate the output. So I've just copied and pasted the code into here. A lot of this is stuff that you guys have already learned at the beginning of the semester. So what we're trying to draw is little buggies. So if we run this, we've got like the left wheel here, the right wheel here, the big character that you sit in here, and then the little pulley part here. So we're using a separate function, which we've called void buggy. It's going to take three inputs, and then it's going to use those three inputs to determine the position X, the position Y, and the size of the buggy. So if you're a little bit confused about how all of these are drawn, just try and draw it out on a piece of paper and see if that helps you understand it, but I'm not going to go through it because you guys have already learned this stuff. So we'll just come up here and kind of analyze what variables we're using and whether they're local or global. So we can see here that we've got no global variables. Let's make a note of that here. And instead, we're going to use our local variables in the function to control what's happening. So we'll make a note down here and we can see that we have a local PX, a local PY, and a local scale. So these are all variables that we can use as inputs. So then when we come up here to column, we can give them different values every single time. So when we call buggy 40, 23, we know that PX is going to become 40. PY is going to become 20. The scale is going to become three. It's going to draw our buggy here. And then once we get to the end of the function, it's going to say, OK, I'm done, you know, these these local instances are done. Then we can come up here and we can call it again and we get new local instances and we're going to assign new variables to those local instances. So it's very scalable in how we write it in that we can give it new variables each time without having to redeclare the variables every time because we've declared these variables here. And then we're just going to reinitialize them all in one function call. So that's why it's really good to use local variables. And here we'll go through a quick example on if we were to make all of these variables, global variables. So let's say I take these away and I no longer want to use local variables. And instead, I'm just going to call the function and it's going to use global variables to draw it. So here I can initialize our PX, our int PY. And then our int scale and then I can come into here and this no longer takes inputs. So instead of creating and using local instances, now we have to come into here and we have to set what we what we wanted our values to be before we call the function. So I want to say I wanted my PX to be equal to 40, like how we had before. We want our PY to be equal to 20. And we want our scale to be equal to three. All right, so we've now declared these three variables and then we call buggy. Now we come into buggy and the function buggy says, do I have local instances of PX, PY and scale? And it doesn't. We haven't created local instances for it. So it has to use the global instances, which are up here. Well, now we go to call the second buggy. But then it goes in again and says, I have no local instances. I have to use the global instances, but we haven't changed those global instances yet. So now I have to come in after I call this first buggy and change our global instances for the second buggy. So our second buggy had a PX of 300, PY of 20, and a scale of one. So now we change those global instances, we call buggy again, buggy uses those global instances, and then after that's done, we have to change our global instances again. So now we can run this and we get the same output, but it took a whole lot more time to write it out. And then it takes up way more space and it's really confusing to see all of this and say, okay, wait, what's going on? So that's only using these three global variables. So that's one reason that if you can use local variables, it's a lot better to use those local variables. Another good reason of why we wanted to use local variables is because now if I wanted to take this function buggy and say I have another piece of code where I also wanted to draw a buggy, I can't just copy and paste this into that function or into a different piece of code because now it's asking for these variables that I haven't grabbed. So if I just copy and paste this over, I'm going to get errors because it's going to say, well, you haven't declared these variables for me. I don't know what they're equal to, I can't do this. So then I have to come back into here, I have to say, okay, I got to find these local variables, I've got to grab these values, and it just gets really confusing. Once you get heaps more code, it's just too much work. So by having those local instances, I could copy and paste this function into a different piece of code that I have, into a completely different sketch, and it would still work because I've initialized those variables or I've declared those variables in the function instead, and then I can declare them when I call the function. And I know that, okay, whatever inputs these are, whatever it says, that's what they're going to be. So in talking about which one is more reusable, using local instances is definitely more reusable. And there are instances when you're going to need to use global variables over local variables. So if I had, say I wanted to move the buggy, then I would have to access whatever variable is moving or animating that buggy, I would have to use that here and draw. So if I ever have any other functions that need to use a variable, then it would need to become a global instance, so that every function that needs to see it can see it. But in this instance, there are no variables that really need to be used in more than one function, they only really need to be used in this function buggy. So it's much more, or it's much better to use local instances in this example. Another reason for this is if we now try to introduce a new function, say a function person, I'll just copy and paste this into here. So if we now try to create this function person, I can declare the same variables and I know that the person's going to have an x position, a y position, and a scale or a size. I'm going to say I want to try and put these people into the buggy. Right now I can use these same variables and then I just insert the variables when I call them. If I didn't have, if we go back to our example of using global variables, I'll just bring back our example with global variables. So this is how we did with using only instances of global variables and no local variables. So if I were only to use global variables, then it would look something like, like this, right? So that's just me taking these inputs and putting them into the function as global variables. But then this gets really messy because now every time we go through again, we've got to reinitialize the px, the py, and the scale every time we want to call a person. And that just gets really long, really ugly. I could try and create an example with like new variables. So this person has a different a different variable for its x position, a different variable for its y position, and a different scale. So we've got person x position, person y position and person scale. But now I've just introduced three new global variables that I probably don't really need. So in this instance, it's much better just to create local instances and use those instances. Let me fix this up a bit, looks a little bit messy. So we'll bring back our instances with only using local variables. There we go. So now I can clearly see that this function uses three variables. And I know what they do. I can clearly see that this function also uses three variables. And I know what it does. And then every time I want to use these functions, I just give it its values. So now I don't have to declare any extra variables. So hopefully this made a little more sense. Again, you can go through and use the debugger if you're still a bit confused. But just keep practicing with it and it'll make more sense.