Function with Parameters:
Syntax:
function name(parameter 1,parameter 2,...)
{
// set of statements that will be executed
}
In many cases we pass parameters or arguments to functions, these arguments will be
used inside the function for required calculations.
For an example we will use two numbers to add,
subtract using function.
Here we write separate function for each operations add, subtract.
Example Code:
<script language="javascript">
function add(number1, number2)
{
var c = number1+number2;
document.write(" ---- This added value is ---- "+c;
}
function sub(number1, number2)
{
var c = number1-number2;
document.write(" ---- This subtracted value is ---- "+c;
}
var a = 7;
var b = 3;
add(a,b);
sub(a,b);
</script>
Result:
Here you can clearly see that the two functions where invoked as "add(a,b);" and "sub(a,b);"
where a and b are defined variables. You can even call the function directly with the variables
as say "add(9,1);".
A function can be invoked any number of times with any proper value.
|