
To perform a specific task we combine a group of statements within braces{} or a block. This is known as Function. Functions are used to perform operations. Functions are reusable code. Any time when we need them we invoke the function inside our program. A complex problem can be easily solved with the help of function.
We have built-in functions and user-defined functions in JavaScript like another programming language.
Advantages of using Function
- By using the function we can avoid rewriting the same logic again and again in our program.
- Functions are reusable. When we need them we can easily call the function.
- Big problems easily solved by functions.
Declaration of Functions in JavaScript
Below is the syntax for declaring function in JS
Syntax
//Declaration of function without parameter
function FunctionName () {
// function body
}
//Declaration of function with parameters
function FunctionName (parameter1, parameter2,..., parameterN) {
// function body
}
Here function
is a keyword which is used to declare a function in JavaScript.
Within {} , the logic of the function is define.
Example
function fun() {
console.log("Welcome! to the JavaScript Tutorial");
}
// fun is the name of the functions
How to invoke a function in JavaScript ?
To use the same function again and again, we can easily invoke the function.
Syntax for invoking/Calling of a function
//Calling a function without argument
FunctionName();
//Calling a function with argument
FunctionName(argument1, argument2,..., argumentN);
Function Parameters
A parameter is a value that is passed to the function while calling it. To use the parameter, we must define variables which are used to store the argument data.
Syntax:
//Declaration of function with parameters
function FunctionName (parameter1, parameter2,..., parameterN) {
// function body
}
//Calling a function with argument
FunctionName(argument1, argument2,..., argumentN);
Difference between Parameters and Argument
when value is passed while Declaring a function is called Parameter
when value is passed while Calling a function is called Argument
Example1 : To add two numbers with the help of function using parameters.
// declaring a function
function addNum(i, j) {
console.log(i + j);
}
// calling function
addNum(10,20); // 30
Example2 : To subtract two numbers with the help of function using parameters.
// declaring a function
function subNum(i, j) {
console.log(i - j);
}
// calling function
subNum(10,20); // -10
Function with return value
With the help of return
keyword , a function return value from where it is called.
If a function contain return keyword then it must be the last line of the program.
Syntax
function FunctionName () {
// function body
return //expression
}
Types of JavaScript Functions:
- A function without parameters and without returning value
- A function without parameters and with returning value
- A function with parameters and without returning value
- A function with parameters and with return value
Example1 : To add two numbers with the help of function using parameters and using return statement.
// declaring a function
function addNum(i, j) {
return i+j ;
}
// calling function
let sum = addNum(10,20);
console.log(sum);//30
Example2 : To subtract two numbers with the help of function using parameters and using return statement.
// declaring a function
function subNum(i, j) {
return i-j ;
}
// calling function
let diff = subNum(10,20);
console.log(diff);// -10
Scope in JavaScript
Scope indicates the visibility of the variables. It tells us the lifetime of a variable in our code.
Two types of Scope in JS
- Global Scope
Those variables which are declared outside the function have global scope in the program. A global variable can be accessed anywhere in our code. The lifetime of the global variable is throughout the program.
let Var = 20; // Global Scope
function fun() {
Var = Var+1;
console.log(Var); // 21
}
fun();
console.log(Var); // 21
- Local Scope
Those variables which are declared inside the function have local scope in the program. A local variable can not be accessed anywhere in our code. The lifetime of the local variable is throughout the function in which it is declared.
function fun() {
let s = 10;
console.log(s); // 10
}
fun();
console.log(s); // ERROR s is not defined
Functions in JavaScript increase the readability of our code. Instead of rewriting the same logic again and again in our program, we can use functions. Functions are used to perform specific operations. We have also inbuilt functions which are already defined by the developers and for our convenience, we can define our own functions.
Check this also Loops in JavaScript.
Article Contributed By : Ankita Kataria.
Thank you !!
Pingback: Object Oriented Programming Concept in JavaScript — CodeShruta
Pingback: Closures in JavaScript — CodeShruta
Pingback: Modules in JavaScript — CodeShruta