Home > DeveloperSection > Articles > JavaScript Functions

JavaScript Functions


JavaScript JavaScript 
Ratings:
0 Comment(s)
 1984  View(s)
Rate this:

JavaScript Functions

Introduction:

Functions provide us Reusability of Code. We can create a function one time and use it every where by just calling it by name in Java Script. It is also available in other languages like C and in Java as Methods, but in Java Script it is known as functions only.

It also facilitates us to divide our big program to smaller components which is available in modular approach. This approach can also be followed in Java Script. There are alert() and write() functions which are predefined and written in core Java Script we only have to use it by passing parameter to it, but if we have to write every time we want to work with alert  function it will create problem and will take a lot of time, so we can write function in Java Script and use it where ever and as many time we want in our program.

Syntax:

function FunctionName (parameter-list)

{

// Code to be executed

}

Note: The function must be written in lower case other wise our code will not execute properly.

Parameter list is used when we need to pass any parameter, otherwise it is optional part. In this article I will show you by example how to work with function without passing any argument, and by passing argument.

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <script type="text/javascript">

        <!--

        function firstFucn() {

            alert("This is the first function");

        }

        -->

    </script>

</head>

<body>

    <script type="text/javascript">

        firstFucn();

    </script>

</body>

</html>

When our code will run our function will be called and we will get a message on the alert box as.

JavaScript Functions

Calling a Function by Passing Argument:

When working with functions by passing arguments, we mean that we are passing some argument along with the function. Suppose we want to add two numbers and there is a function for adding two numbers then we have to pass that function two numbers on which computation can take place.

Syntax:

Declaration of function:

function myFunc(var1,var2,var3)

{

// Code to be executed.

}

We are declaring the argument as Variable for eg. var1, var 2, var 3, but we can pass parameter as much as we want.

We can send as many arguments we want.

Syntax for calling Function when sending argument:

myFunc (arg1,arg2,arg3)

Example depicting working

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <script type="text/javascript">

        function funcParameter(EmployeeName, Designation) {

            alert("Employee Name is " + EmployeeName + " and Designation is " + Designation)

 

        }

    </script>

</head>

<body>

    <div>

        <input id="CallFunc" type="button" value="Press! to call function"     

       onclick="funcParameter('Amit','Manager')" />

    </div>

</body>

</html>

 

JavaScript Functions

Conclusion:

This article will give you the concept of using function with argument and without argument, which is very important part of Programming Languages.


Don't want to miss updates? Please click the below button!

Follow MindStick