This lesson is about functions, and this is at the very heart of JavaScript and what JavaScript is
all about.
We're going to be covering what functions are and how to use them and how you can create functions.
So functions are blocks of code, their reusable code, one of the fundamental building blocks of JavaScript.
And in order to use a function just like variables, you need to define them somewhere within the code
in order to call upon them.
Some examples about functions that the module a developer network.
So there's a quick example of a function.
So defining the function, giving it a name and then passing in an argument, running some code.
And you also have an option to return information and you also have an option to return.
And we are going to be looking closer at that later on.
And they've got some examples of functions.
So here they have an example of a function that takes in a parameter and then it updates that parameter
adding in amache into the object.
And of course, more on objects coming up later.
There's also function expressions.
So these are functions, but they're written with the same way as a variable where we're declaring the
name of the function and then it's got the function contents afterwards.
So those are two different ways to write functions.
We are going to be covering a little bit more on these both of these as we go through the upcoming lessons.
Quick example of a function so parameters and then the code that needs to be executed is within the
curly brackets.
We do have a challenge for you and of course, it's in regards to functions.
So what I want you to do is create a function to output a message from a variable in the console.
So in VOC, the function three times every time increasing the value of the global variable that counts.
The number of times this function is run, create a function to output into the console the results
of adding ten to a number that is passed into the function.
So we've got two separate challenges here and you can see the output that we're expecting within the
console.
And this is a quick peek at the code that we're going to be writing can go ahead and pause the video,
create the solution.
So the first challenge is up here and we invoked the function three times, incrementing the counter.
Second one where we're creating a function, where we're adding a value of ten to the number.
So we're passing in a number parameter into there.
And then we're using that as a number and setting a value in the console that we can take a look at.
So pause the video, open up your editor, write it out and I'll show you the solution coming up.
So if you had an opportunity to try out functions, so first we need an output that we're going to declare
a variable and this one can just be a string value holding hello world.
And we're also going to set up a counter and that counter is going to start at zero.
So these are the global variables that we're going to work with and then within the function we're going
to pass.
So we're going to call a function called welcome and pass in the parameter of output into it.
And then down here is where we're going to set up our welcome function.
So it's going to be expecting a parameter.
And this doesn't have to be the same.
It doesn't have to be the message.
You can call this whatever you want.
And then this is how we reference that parameter within the block, a code within the function.
So because counter is global, we can always increment counter and we can also create variables that
are within the function itself.
So we're taking the value of counter and times run.
And this will be our message that we're output into the console so we can see that we're passing in
parameter.
So that's the output of the parameter.
So it's going to say Hello World.
And then we're also outputting our temp, which is our message into the console.
So we're going to have two entries into the console.
So we see the first one is Hello World.
And then we see what one times run where we can call to the function we're passing in a parameter.
So this time the parameter is hello.
So we see that hello gets output.
As for the console there, that's taking whatever parameter we've passed in, it's putting into the
console and then it's incrementing the value of counter because this is global and then we're outputting
that message within that temp area.
You can always change the value of the parameter that's coming in.
So then every time that can be something different.
And then, of course, when we refresh it, we go back to one, because this is written in the code.
The other chap, the other part of the challenge, this was a two part challenge is where we need to
set up a number.
So let's just set up a random number here and then creating a function that we're going to call add
ten and then passing in that value of my number.
And now we need to create the function.
So this function is going to take a number value and it's going to simply add 10 to it and output it
to the console.
So you can also set up temporary variable as well for that.
Make sure that whatever value is coming in is.
A number which we know it is so it is 50 and then we're going to console log the value of temp plus
10.
So every time, whatever number we want to pass in.
So this can be any number at all.
And every time we pass and a number, what the function is going to do is it's going to take that number
value that's being passed in, add 10 to it.
It doesn't matter what number we pass in or how many times we run it.
And that's the whole purpose of a function using the parameters.
So you don't have to have parameters.
You can just run the functions as on their own as well.
And before we conclude, I realize that we do need to output welcome three times.
So we're going to be invoking the welcome function three times so we get our output.
Hello world one times run.
Hello World two times run.
Hello World three times run and then we're passing in our number and returning back 60.
So we're matching all of the criteria that we set out.
So coming up next, even more on functions, how you can pass in multiple parameters using the default
argument with functions.
So that's still to come.