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.
A javascript function is executed when some theme invokes it but invoke a mean cause it.
So in order to activate a function after it has been created you 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 a very popular one is the 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
piece by using the function keyword functions often takes arguments arguments are bits of data that
the functions need in order to perform or do their jobs.
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 created function you basically have to
type in the keyword function followed by the name you want to call the function when you are creating
function.
It's very nice to name your functions as closely as possible to the task that the function is meant
to perform.
See my function of code it add num which indicates that this function is going to be used to add numbers
together.
So when you create a function you furthermore have to type in the word function followed by a space
then the name of the function and then you have to have parentheses 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 pass the argument inside the parentheses.
If there's more than one argument you separate them by a comma you can see here number one is an argument.
Number two is referred to as an argument.
And once you specify the arguments if there are any inside the parentheses and then you have this curly
braces you've called 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 I've also got a return statement here.
Basically the return statement tells the browser to return the result variable out of the function.
So it is available to use.
All right.
This is necessary because variables defined inside functions are only available inside those function.
This is called Variable Scope.
So when you create the variables variables can be available throughout the application or throughout
the scope which is known as global so if you create a variable and you declared outside the function
that makes that variable global.
That means you can use it anywhere within the block of code but if you define a variable inside a function
like that on here I've defined a variable code result and offset that variable result to equals to whatever
value is passed as number one and add added to value past as number 2.
So this variable is no core to this function so it can only be accessed inside this function.
So when a variable exists inside the function once the function has been executed the variable basically
is destroyed or you know it's it becomes on you you can't use it again wants to function has been called
thus it is served its purpose.
However if you want the variable to be available you have to use the written statement that way it would
tell the browser to return the result of the variable out of the function so that it is available to
use.
You only do this for variables deci defined inside the function.
The unknown has a local variable scope so variables defined outside the function on global.
So you can access them from anywhere within the US within the javascript code.
Whereas variables defined inside a function like I have done here are referred to as a local scope variable.
So here I have defined this variable court ad add num now to make or to invoke this function.
Now that I've created it all I need to do is call the function button his name which is and NUM 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 say 10 comma 20 and I close parentheses and I 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 passing
two arguments.
You can see it sees me having to rewrite the function again I've already written it defined it.
All I need to do is just call it passing whatever arguments I need for the function.
So for example you can create the same page maybe for multiplication 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 a function you don't have to write all the code again or you need to do is just
call the function by its name and passing whatever arguments you need to do.
Like I've passed in here and then you know the function executes if I want to add passing more argument
and I just call the function by e name and policy it's a 6 and 6.
Now at 6 and 6 together it will return to 12 because I've already done written all the the functions
here.
So all I need to do is call it base name and passing the arguments so that's basically how you create
and use a function functions a very powerful concept in JavaScript.
I'm only in a 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 video.
Thank you for watching and bye for now.