Hello and welcome.
In this video I will be introducing you to javascript functions.
Functions are a way of packaging functionality that you wish to reuse.
So a function basically can refer to a block or a piece of code that is designed to perform a particular
task.
Jemas group function is execute it when some theme in it back evil a mean cause it.
So in order to activate a function after it has been created.
We need to invoke it or call it.
And usually you do that by its name.
Javascript has many built in functions that you can use very low on his alert which enables you to create
pop up box messages.
Javascript also enables you to create your own functions and the way you create your functions in Javascript
is by using the function key word from chao's often takes arguments and arguments are bits of data that
the functions need in order to perform or do their job.
And these arguments are placed inside parentheses which are separated by commas.
If there are more than one argument.
So inside my console I have created a function and the way you create a function you Busy B have to
type in the keyword function followed by the name you want to call the function.
When you create creating function it's very nice to name your functions as close as possible to the
task that the function is meant to perform.
See my function I've called it and none which indicates that this function is going to be used to add
numbers together.
So when you create a function you have to type in the word function followed by this space then the
name of the function and then you have to have parent.
This is sometimes the parentheses can be empty but you must have that in order to create a function.
And then if you've got any arguments you parse the argument inside the parentheses.
If there's more than one argument you separate them by a comma.
You can see here number one.
Here's an argument not to refer to as an argument.
And once you specify the arguments if there are any inside the parentheses you have this curly braces
you call the opening curly braces here and the closing curly braces and inside in between the curly
braces is the block of code that has to be executed when the function is invoked or called.
Notice here also caused a return mistake here.
Basically the return statement tells the browser to return the result variable out of the function so
it is available to use right.
This is necessary because variables defined inside functions are only available inside does function.
This is called variable scope so when you create variables variables can be available try out the application
or out the scope which is known as global so if you create a variable and you declare it outside the
function that makes that variable global That means you can use it anywhere within the block of code.
If you define a variable inside a function like I done here I've defended variable called result and
I've set that variable result to cause to whatever value is passed as number one and add added to value
passed as number two.
So this variable is local to this function so he can only be accessed inside this function so when a
variable exists inside a function as a function has been executed variable basically is destroyed.
Or you know it's because on you you can't use it again.
China has been called that.
It is served its purpose.
However if you want the variable to be available you have to use the written statement that way to tell
the browser to return the result or the variable out of the function so that it is available to use.
You'll only do this for variables.
This site defined inside the function then known as a local variable scope so variables defined outside
of function global.
So you can access them from anywhere within the US within the javascript code whereas variables defined
inside function like I have done here referred to as a local scope variable.
So here I have defined this variable at the nump.
Now to make or to invoke this function not I have created it.
All I need to do is call the function his name which is add on and then I have to you must add the parentheses
and then I'm going to pass it to values because I've got two arguments there.
So if I see 10 comm. are 20 and I close the parentheses and the press Enter now add 10 and 20 together
and give me 30.
All right so any time I want to use this function all I need to do is call the function by his name
and then pass in two arguments so you can see he sees me having to rewrite the function again.
I've already written it to find it.
All I need to do is just call it parsing whatever arguments I need for the functions.
For example you can create the same page maybe on what application or anything.
This is just a basic illustration to show you the importance of functions so you can create functions
for different kinds of task or to do different things.
But once you've created the function you then have to write the code again or you need to do is just
call the function by its name parsee whatever arguments you need to do.
Mike I've passed in here and been at the function execute.
If I want to pass in more argument I just call the function is the name that passes in C at 6 and 6.
Now add six the six together.
It will return 12 because I have already done it in all the functions here.
So all I need to do is call it base name and pass him arguments so that's basically how you create and
use function functions are a very powerful concept in javascript.
I'm only barely scratched the surface here.
This is just an introduction to Javascript function.
There's a lot more to it.
So that's it for this too.
Thank you for watching and bye for now.