Block of statements or code is called a function.
There are two types of function
- System defined function
- User defined function
System defined function
A function defined the system like parseInt(), write() are called system defined function.
User defined function
a function defined the programmer or user is called user defined function.
In java script functions are created using function keyword.
syntax
function function_name(){
code...
}
example
function message(){
document.write("Hello");
}
Run the function
To run the function, we need to call the function with the function name.
example
// function call
message();
Non parametrised function
A function without parameter are called non parametrised function.
example
function sum(){
var a,b,c;
a=10;
b=20;
c=a+b;
document.write("Sum:"+c);
}
Parametrised function
A functin defined with a parameter are called parametrised function.
example
function sum(a, b){
var c;
c=a+b;
document.write("sum:"+c);
}
Parameters v/s Arguments
Variables write at the function definition are called function parameters
whereas
Variables pass to the function call are called function arguments.
or
arguments are called real values.
Function with return statement
To return the value from the function, return statement is used.
example
function sum(a, b){
var c;
c=a+b;
return c;
}
var z;
z=sum(10,20);
document.write(z);
Default parameters
Value assign to the parameter are called the default parameter.
function sum(a=0, b=0){
var c;
c=a+b;
document.write("sum:"+c);
}
sum(); // 0
sum(10, 20) // 30