Okay, here. We are we are now going to talk about functions, and what do we mean by functions?
Do we know what functions are is this something we're even familiar with? In fact yes, let's just begin here
umm a simple little place, in a simple little place where we just wrote something like this:
background(0);
This is us calling a function. We, at one point, we might have called it a command, but really it's a function.
We're calling a function. What function are we calling? It has the name background. It needs arguments.
Maybe we say background zero, for a black background, 255 comma zero comma zero for a red background, etc etc...
This is the function we're a calling.
Where does that function come from? Well it comes from Processing.
But it doesn't just sort of exist by magic or just because: well of course you say background, you get a background. It exists because
somebody, somewhere, perhaps who goes by the name Ben Fry.
Defined this function.
Somebody defined what it means, what code should be executed when
you, the Processing user, the programmer, the person writing your Processing code, call the function background.
What would happen if... and then the same thing is true for if I wrote rect.
rect(0,0,100,100);
How does this exist because somebody defined the function rectangle. rect(). Okay, what if I were to write this: flower....
flower(300, 20, 10, 10, 10, 10);
That must be nice we could write that code and then we get a lovely flower in our program wouldn't that be nice?
Let's go in fact run this for a moment and see what happens.
Going to make a new sketch here. We still have our little bouncing ball. We'll come back to it
I'm going to make a new sketch here
and say ok...
I know I could say background(0); and now I could say flower
100 100. I'm really looking forward to seeing this beautiful flower...
It's gonna be lovely... okay of course we know... that was
a little bit of time wasted there
Because you knew what was going to happen. The function flower, int, int, does not exist.
It does not exist because no one has defined that function. I am here today to say to you:
This is the moment where you can begin to define your own functions, right?
We can start to say: You know what, nobody wrote a function for how to draw a flower in
Processing. I'm going to write that function, and how do we do that? Well here is the syntax for how we do that?
We want to write the flower function...
We need to say this... well okay, so the way a function is defined is by saying first
we have to give it a return type.
Then we have to give it a name of the function.
Then we give it some
number of
arguments, oh this is terribly disorganized the way I did this whiteboard right now. We're just gonna have to live with this okay
Then we need an open curly bracket and then a close curly bracket.
This is the syntax for how a function is defined.
Return type, the functions name, some amount of arguments perhaps
and then a curly bracket, an open curly bracket, this might be a little bit cut off by the camera,
I apologize, and a closed curly bracket. This syntax might actually look a little bit familiar to you.
I mean first of all we recognize this as a block of code. It has an open curly bracket and a close curly bracket. if statements, loops
setup(), draw(), all these things, functions that we're defining, have, are blocks of code. Open curly bracket close curly bracket.
Interestingly about this...
This looks perhaps quite a bit like...
Void draw, parentheses, parentheses, open curly bracket, close curly bracket.
Return type is void, the name of the function is draw,
parentheses for arguments, no arguments, open curly bracket, close curly brackets. So incidentally we have been defining our own functions.
We've been defining the function setup() and draw(). Sometimes
mousePressed(), sometimes keyPressed(), and the thing about this these are special functions. These are special functions that Processing asks us to define to
control the flow of the program. We know a lot about this. Processing starts with setup(), draw() happens,
it loops over and over again these are functions that we're asked to define and
Processing calls them for us. What we're now doing is defining a new function, say called flower, that we want to call. Okay?
And also, by the way...
We now we're at this very exciting moment where we are going to learn
what the word void means. You've been writing it all in your code wondering what that is... Well,
what is void? Void is a functions return type in fact when a function has no return type you say void.
So we're going to ask ourselves,
in I think two videos from now, what it would mean for a function to have a return type.
But having a return type, a function that has a return type gives something back. For example if you want a random number from
Processing you say: "Hello, well you wouldn't say hello, you call the function... hello, no, you should always say hello,
it's just polite but unfortunately it probably won't know what that means, but in any case you say a
float R equals random. That function random returns a number back to you. The function rectangle does not return anything.
Background does not return anything, it performs a task. So functions that perform a task that executes some code without
returning an answer so to speak at the end of have a return type of void.
I'm sorry for kind of going off on that a little bit
we're going to get to the specifics of that in a video, which will be all about return types. For now
we can say we're running functions without return types. They have a void return type. So to write that flower function... void
flower
now
I'm going to simplify things and say just for the moment now, that we're going to deal with functions that have no arguments.
Clearly it's useful to have a function with arguments. Draw a black background as opposed to just saying draw a background. Get to say, black
red, blue, etc...
Draw a rectangle here with this width and this height. That's much more useful than just say draw a rectangle.
But we're going to start with a simpler case with our function all it's supposed to do is draw a flower.
This is how we would define it. All of the code
for drawing that flower should go inside the open curly bracket and the close curly bracket. When we get to this line of code in
our program, this line of code
it looks a background in the Processing API because it's
predefined in Processing. This line of code looks up rectangle in the Processing API because it's
predefined in Processing. This line of code will now look in here, right?
It's going to say ah I got a call that function. Where is the function definition?
Here is the function definition. Where do you write that function definition in your code?
Well, you can write it really anywhere you want. I'm going to put it right here.
When I say anywhere you want it could be, in between setup() and draw(), it could be above setup().
Typically you put it below setup() and draw() that might be sort of the standard. The key thing is
flower, the function flower, is its own block of code.
It is its own block of code. It cannot be inside draw, cannot be inside, these are
distinct blocks of code that are separate. And when we run this program now...
Tada...
Ops, all the way at the bottom right-hand corner here. We get a flower...
No we don't. So we don't get anything because we didn't actually put any code in that function.
But if I were to give us a little bit of a size here
so that the window is a little bit bigger and say I know our flower is going to be this beautiful
red circle
Now obviously this is somewhat pointless in the sense that we don't really have something that looks like a flower
But you could imagine if you were to invent a very complex sophisticated algorithm with several lines of code you could make it modular.
This modular piece that all lives inside the flower function and
is then called from draw(). It's allowing you to really just organize your code in a clear way
this.. now... this video went on a little bit longer than I expected so what I'm going to do in the next video...
You can actually try this for yourself first. Is take something you've written in Processing before and see if you can figure out okay
well this part is the part that draws the flower pattern, this is the part that makes the flower pattern bloom.
Can you write a draw... display flower function? Can you write a bloom function? Can you take your program and divide it in to parts and
separate those parts into distinct functions that you write below draw()
but call them in draw(). Give that a whirl. I haven't shown you too much as to how to do that
but the next video is going to very specifically...
To take a program and divided into parts, okay?
And with that I say I bid you farewell