JavaScript Function
JavaScript Function
• What is function
• Variable in function
• Problem 1
• Problem 2
What is Function in JavaScript
Function is the set of statement enclosed in a specific block
of code to get required functionality to perform specific task
There are two steps to work with function
We create Function:
1. Create a function
• If we want to repeat a code again and again
2. Call that function
• If we want to save our time
Creating a function
• If we want to perform a specific function
function test(){
document.write(“I am Function”);
Syntax
}
function funcName(){
Calling a function (Invocation)
//write code here
test()
}
Arguments / Parameter in Function
Arguments / Parameter
Any value or variable pass to function using parentheses to use inside the function body, is called function
arguments / parameter.
Example:
function test(a,b){
document.write(a+b);
}
test(2,3)
Different Types Arguments in Function
• One argument
• Multiple argument
• Default argument
• Keyword argument
• Position argument
Difference Between Parameter and
Arguments
Parameter and Arguments Example
Parameter is the local variable that is used in the function test(a,b) // Parameter
parentheses of function definition after the name of function
{
Argument is the actual value that we want to send toward
document.write(a+b);
function on the time of calling a function
}
Parameter are the variable used a receiving for value
test(2,3) // Argument
Argument are the value that is send to parameter variable
Function Variable in JavaScript
Variable in the function
You can create any variable in the function body that will be
consider a local variable.
Local variable means, that variable will be use only inside the
function not outside of the function
Example
function test(a,b){
var c = a + b
document.write(c);
}
test(3,4)
JavaScript Function: Problem1
Write A Program In The JavaScript To Get Two Number From User To Pass Function To
Perform Arithmetic Operation On That Number
JavaScript Function:
Problem2
Write A Program In The JavaScript To Get One Number From The User, Pass That Number
To Function To Generate Table Of That Number