Function is a set of statements recognized by a name and can be used as and when required (it is reusable).
A function can accept parameters and can return a value back to the calling position. The parameters accepted by a function can be used in the code of that function. |
Syntax: |
function functionname(var1, var2,..)
{ lines to be kept under this name return(variablename) } |
Example: |
<html>
<head> </head> <body> <script type=”text/javascript”> function max(m) { var x,y y=m[0]; for(x=0;x<m.length;x++) { if(m[x]>y) { y=m[x] } } return(y) } var m=new Array() m[0]=50 m[1]=12 m[2]=99 m[3]=45 m[4]=120 m[5]=110 m[6]=22 m[7]=106 i=max(m) document.write(“Maximum Value is “+i) </script> </body> </html> |
Understanding program: |
An array is passed to the function and the greatest value is searched in the array and returned back, at calling position a provision to keep the returned value is kept in form of a variable, means whatever value the function will return, will be kept in i variable. |