Loading, please wait...

Parameters

Function Parameters

The function can have one or more parameters.

Here, parameters must be separated by commas within the parentheses.

While passing the parameters, the function is defined as follows:

 

Syntax:

functionName (parameter 1, parameter 2, parameter 3)
{
   Block of code
}

 

Code:

<html>
<body>
<h2>The function</h2>
<script>
function demo(name)
{
document.write(name + "<br>");
}
demo("Nitin");
demo("Chirag");
demo("Nakul");
</script>
</body>
</html>

In the above example, the function has one parameter named as a name.

So, by calling a function we must pass one parameter in order to get the program get executed.

 

Output:

 

 

 

Multiple Parameters

We can also define the function with multiple parameters separated by a comma. So, Function can have one or many parameters but by calling a function we must define an equal number of parameters as we define in the function.

 

If we pass less number of parameters as compared to the function parameters then it assigns an undefined value to rest of them.

If we pass a number of parameters as compared to the function parameters then it ignores the rest of the parameters.

 

Syntax

functionName (parameter 1, parameter 2)
{
   Block of code
}



 

Code:

<html>
<body>
<h2>The function</h2>
<script>
function demo(name, age)
{
document.write(name + " is " + age + " yrs old." +"<br>");
}
demo("Nitin", 20);
demo("Chirag", 22);
demo("Nakul", 23);
</script>
</body>
</html>

 

Output:

 

 

 

In the above example, the function has one parameter named as a name.

So, by calling a function we must pass less or more parameters then rest of the parameter will be described as undefined or ignored.