Use Of Functions.

Functions are used when same process is repeated many times

e.g.
<script>
function ask()
{
var x = prompt("Enter a number ", " ");
var y = prompt("Enter another number ", " ");
var z = x + y;
alert (z);
}
</script> This function can be used as many times required in the main program.


Why Functions are important?

1. Variable passing becomes very flexible.
2. Functions can be used in combination with event handlers.

e.g.
<html>
<head>
<script>
function ask(myform)
{
alert (myform.elements[0].value);
}

</script>
</head>
<body>
<form>
<input type="text" name="1" size="20">
<input type="button" value="ADD" Onclick="ask(this.form)">

</form>
</body>
</html>                                      

Back