Okay, in this video we want to emphasize the concept of:
Reusability with a function. So we're looking at functions with arguments
Before we maybe thought okay?
So let's do this back up for a second. Let's look at the scenario that we're going to present to ourselves which is over here.
Hello, okay, so here's our scenario. We have a nice little Processing sketch.
If you look up here, we've got setup(). We've got draw(). Draws all this code...
We're using beginShape(), endShape(), sending a bunch of vertices that are all hard-coded and
we're getting this star pattern. So we see this lovely star that's being drawn. So we work this out and we know from the last
video that I could say, okay:
Let's take all this code
oops, ah
Come on. I can do it. I can do it. I take all this code. I got it...
I got it, and I could make a function called Star
I could put it all in that function, and then I could call star
from draw().
So we now... This is modularity
We took all that code that has to do with drawing a star and we put it there. Ah!
But I want a second star.
Okay, so let me get a second star. Two stars coming right up. Well
it is drawing the star twice but it is drawing it in exactly the same place. In exactly the same size with exactly the same color.
We don't have reusability. This function
can't be reused in an effective way. It can only be used once. How do we make something
reusable? Well let me ask you this question: When you want to draw a rectangle on the screen...
Do you just say this? No you do not.
You give it arguments.
You say I want a rectangle with this x with this y with this width,
I don't know what I'm doing here, with this width and this height.
We want to be able to do the same thing with star. We want to say I
want a star at this
location:
100 100. I want to be able to draw a star with these arguments.
I could add a color to it. I could add a size to it, etc etc, But I want to draw a straw, straw, a star
with these arguments okay. So how do we do that? Well, we know the way we define a function is:
return type, function name, followed by parentheses, followed by open curly bracket end curly bracket.
So we know this star function has no return type. So it's void. We know the name of the star function is the word star.
Now we have a big open question here.
What do we put in there?
Look at this.
This is how we are asking to call the star function. We are saying I want to give the star an x location.
I want to give the star a y location. How do we give it to them? Well interestingly enough...
Remember a few videos back and hopefully there's some automatic way eventually to be able to click right to this video. We talked about variable scope?
Local variables... Sometimes we need a variable only for the purpose of a certain block of code. This is a case.
Arguments to a function are local variables.
Only to be used for this, I'm kind of like, oh at the edge here, you can't even see me. This is.. I kind of messed us up. We're just going with it.
Arguments to a function are local variables. Local to the use of us of
a function. We want to send this value 100 into a local variable here. How do we do that? We actually
declare a variable with a type
and a name.
We declare a variable x with a type float comma and then a variable y with type float. As many as we want here. As few...
From 0 all the way to as many as we can sort of practically handle. So what happens this value 100, it makes its way
all the way up here and goes into x. It says if we wrote right here float x equals 100. It says if we wrote
Float y equals 100. This star function now can receive values stored in these variables just for use locally throughout the star
function. So I could say something like vertext at x
comma y. I could say something like vertex at x plus 50, y minus 20. So I could start assigning
vertices... Obviously, I would need beginShape and endShape, and all the color all the rest of the code. I could start defining vertices
according to these arguments that have been received from the calling area. So we are passing these values
they are going into these...
local variables. The function's arguments x and y, which can then be used
throughout the function. Let's look at how we would actually do that in this...
In this example right here. Now, because it would take me a little while, this star
shape is complex enough that I would I need to set all the vertices relative to an x and y variable, I have that already
sort of prepared. It's like a cooking show where, you know, we've mixed all the ingredients
would take a half an hour to cook it. So that we would then like, just pull, pull it magicly from the oven.
It's not like a cooking show at all is it? Whatever. Okay. So, here we go and we have this program here.
kind of
Wrong, no, that's the wrong one. Come on Schiffman... Okay star function. That's what I'm looking to open...
We can see here. I have actually all of this code...
Written with a star shape being drawn relative to x and y. So I can create that star function...
Like this. Now if I run this, it's saying it doesn't know what x is because I need to define those arguments, sorry...
So look at this
we now have our
Processing sketch with our star function that receives two
arguments. It receives an x. It receives a y and it uses those x and y to define
Where the vertices of that star are relative to whatever argument was passed in. So now I can do something really magical...
I can say: "Give me a star at 100, 100 and give me a star at 200...
300. Let's run this...
Look at this. I now have oh, it's so beautiful.
I now have two stars. I've successfully implemented functions for reusability in this Processing sketch.
Not only do we define what it means to draw a star
But we defined it in a flexible manner that it can receive arguments. The value 100 gets sent into x,
100 to y, then all this code is executed. Then the value 200 gets sent to x and 300 gets set to y
and then all this code is executed and we have two stars on the screen here. So here is an exercise for you...
Here's a couple different exercise for you. One use a loop
with this star function, use a loop to create you know a...
row of 10 Stars... or
make a whole bunch of stars on the screen with a loop. That's exercise number one.
Exercise number two, write this program in such a way that everywhere I click a star would appear.