JavaScript Functions With Return Value

Some times, we need a function should return a value. A function can return a value by using the "return" statement like all other languages. The return statements enables a function to return a value.

          The following examples,shows how a function return a value.

<html>

<head>

<script type="text/javascript">

function product(a,b)

{

return(a*b);

}

</script>

</head>

<body>

<script type="text/javascript">

document.write(product(4,3));

</script> 

</body>

</html>



         In the above program the function product returns the product of the two variables.

            If we declare a variable within a function, they can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables.

          If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed. These variables are called global variables. 

No comments: