Wednesday 1 January 2014

Javascript : Understanding Functions

Javascript(JS):
JavaScript Functions :
A function is a group of reusable code which can be called anywhere in your program.
its help us to eliminates the need of writing same code again and again. it helps
programmers to write modular code. You can break your big program in a number of small and operable functions.

Like any other advance programming language, JavaScript also supports all the features
necessary to write modular code using functions.

some predefined function are alert() , write() , confirm();

JavaScript allows us to write our own functions as well.
This section will explain you how to write your own functions in JavaScript.

Function Definition:
Before we use a function we need to define that function.
The most common way to define a function in JavaScript is by using the "function" keyword,
followed by a unique function name, with some parameters (that might be empty),
and a statement block surrounded by "{}" curly braces. The basic syntax is shown here:

<script type="text/javascript">

function name(parameter-list)
{
  your statement......
}

</script>
Example:

A simple function that takes no parameters called "begin" is defined here:

<script type="text/javascript">

function begin()
{
   alert("hello javscript");
}

</script>
Calling a Function:
To invoke a function somewhere later in the script,
just write the name of that function as follows:

<script type="text/javascript">

begin();

</script>

Function Parameters:
we have seen function without a parameters. But there is a
facility to pass different parameters while calling a function.
These passed parameters can be captured inside the function and
any operation can be done over those parameters.

A function can take multiple parameters separated by comma.

Example:

This time it will take three parameters:

<script type="text/javascript">

function begin(name, place , year)
{
   alert( name + " is from this " + place + "from" + year );
}

</script>
Note: Use + operator to concatenate string and number together.
JavaScript does not mind in adding numbers into strings.

Now we can call this function as follows:

<script type="text/javascript">

begin('Zara', 'bangalore', 1988 );

</script>
To understand it in better way you can Try it yourself.

The return Statement:
A JavaScript function can have a return statement.
if you want to return a value from a function.

For example :
you can pass two numbers in a function and you can return the output of that function

Example:

The function takes two parameters and combine them and return result. Take a look

<script type="text/javascript">

function hello(first, second)
{
   var result;
   result = first + second;
   return  result; // result have the output which will be return where ever this function being called
}

</script>
Now we can call this function as follows:

<script type="text/javascript">

   var value;
   value = hello('hello', 'world');
   alert(value );

</script>

No comments:

Post a Comment