Learning Javascript Part V
So we learned about arrays and objects earlier, this time we are going to take a look at the Functions.If you have'nt checked out the earlier tutorials then i recommend you to do so.
Please open your console in browser to understand better.If you are new to this series then please first go through the earlier posts as this is an follow up series.Every post is short and sweetso don't worry much about it.
Please open your console in browser to understand better.If you are new to this series then please first go through the earlier posts as this is an follow up series.Every post is short and sweetso don't worry much about it.
Need Of Functions
Lets say i want to do some thing like lets say a + b , but i want to do it five times at different place in my code.So one way to do this is that I copy-paste it 5 times at the places i need it.But What if I am working on a much bigger code and want it thousand times, than doing the Copy paste is not a good idea.
Here comes the functions, simply a function is a way to bundle code so that it can be reused.In this post we will learn how to make function and use them.Lets get started.
A Basic Function
Syntax of a function
A function have two main things to understand...........
1.) Defining the function
Defining the function simply means that we are creating it.Towrite a function the function keyword is used followed by a name, a set of parentheses, and a block between curly braces.
or
var myFunction = function()
{
console.log("This is my first function");
}
The return causes the function to exit and return the specified value to the place where the function was called. The above function can be written with return keyword like this.,
function myFunction()
{
var msg = "This is my first function";
return msg;
}
Then you can call it like this.,
console.log(""+myFunction());
function myFunction()
{
var msg = "This is my first function";
return msg;
}
Then you can call it like this.,
console.log(""+myFunction());
2.) Calling the function
Calling the function is not a big deal, you just have to type its name followed by a set of parentheses like this
Function parenthesis and argument passing
You may ask why there are parenthesis after function's name. These parentheses are used for passing arguments to the function. Argument is the data that we may need while performing some operation.
Let's understand it with an example.
or we can write it as follows.,
function add(a,b)
{
var c = a+b;
return c;
}
here, a and b are parameters which are passed to the function and then used in Function body.While
calling the function we can pass the two arguments to the functions as we have two parameters.
So now i think you got an idea what an argument is and how parameters are useful. One thing you should know that parameters are themselves not a variable.
We have learned the basics of function till this point.Please share this post and ask in the comments if you have any questions.
Thank you for giving your precious time..................................
Learning Javascript Part IV
Learning Javascript Part III
Learning Javascript Part II
LEARNING JAVASCRIPT PART- 1
Comments
Post a Comment