Wednesday, 27 April 2016

Javascript Functions


JavaScript functions are used to perform operations. We can call JavaScript function many times to reuse the code.

Syntax:
function functionName([arg1, arg2, ...argN]){
 //code to be executed
}  

Example:
function firstFunction(x1, x2) {
    return x1 * x2;              // The function returns the product of x1 and x2
}


Function Arguments

We can call function by passing arguments. Let’s see the example of function that has one argument.


Example:
function getVal(num){
alert(num*num*num);
}  

Function with return value

Example:
<script> 
function getData(){
return "hello first program";
}
</script> 
<script> 
document.write(getData());
</script>

No comments :

Post a Comment