In this video I want to talk about functions, and
we are now well into our sort of intermediate JavaScript.
And functions, they're a little bit more complicated than what we've learned so
far, but they aren't too bad.
So, a function is basically a block of code
that is meant to perform a specific task.
So a function sits there until something else calls it, or we call it invoking it.
So some programming languages call these methods.
In Ruby, they call functions methods.
In JavaScript, they call them functions.
Functions, methods, they're basically the same thing.
So if you're familiar with methods in another programming language,
it's just a function in JavaScript.
So, let's go ahead and just build one here.
Jump right in and we start by calling the function key word.
And that's just function and then we give it some name.
Just call it someFunction for now and then, we put these parentheses
right next to it and we'll see what those are for in just a minute.
Then, do these curly braces again.
And everything between the curly braces, that's our stuff.
So, do something, right?
But everything inside here, that's the actual function.
That's the code that's going to be executed whenever we call this function,
whenever we invoke the function.
So, these parentheses are important, and
we can actually pass stuff into our function from the outside world.
So, if we said myNumber1 and then myNumber2.
Now, we can pass in things into this function and
do something with them inside.
So we can take, for instance, return myNumber 1 + myNumber 2,
and return I'm gonna say, functions return things.
You know, we're gonna run the code and
we're gonna return the output back to the real world.
So myNumber1 and myNumber2, those are called parameters, and
like I said we can pass things in.
So lets play around with this.
To invoke a function or call a function, we just type in the name of the function,
someFunction, and then parentheses.
And that'll do it.
That invokes the function.
Now if we wanna actually pass parameters into the function, we do it right here.
So I might go 1 and 4.
Now this is gonna pass 1 into myNumber1, and it's gonna pass 4 into myNumber2.
So if we run this, actually let's go document.write
Pass this in.
So we're passing in 1 and 4, and we're adding 1 and 4 so we should get 5.
We got absolutely nothing though, I misspelled function.
My spelling is terrible.
Boom. There we go, five.
So we can flat out call a function,
we can invoke a function like we just did right here, just by calling it.
Or, we can assign the output of a function to a variable, and
then do something with the variable.
So, we can go var myVariable equals someFunction and
then let's pass it 1 and 5.
And then, down here we can just output myVariable.
See what we're doing here?
We're saying assign the output of some function into this variable, and
then just print mVariable to the screen.
So if we save this, we should should get six.
Okay. Reload. Because one and five is six.
So that's pretty cool.
So, why should we use functions?
Well, they allow us to create little programs and
then reuse them whenever we want to.
So, once it's written, you can call a function whenever you want, over and
over again, as often as you want whenever.
You just invoke it with its name in the parentheses like we just did.
So those are functions.
They're really one of the fundamental building blocks of any programming
language, but I think you'll agree that they're not that complicated.
And that's all for this video.
In the next video, I want to talk about if-then statements.