- 5 years ago
- Zaid Bin Khalid
- 8,833 Views
-
9
In this session, you will learn how to define and call a function in JavaScript with the help of an example.
What is a Function?
Function keywords define how functions work in the JavaScript function. Functions are the block of code designed statements that perform a particular task or calculates a value.
Advantages of Functions:
Advantages of using functions are mention below:
- Functions are used to reduce the repetition of code within a program.
- These functions are used to make the code much easier.
- The functions make it easier to eliminate.
Below are the details that will explain “how to define and call functions” in your JavaScript.
Defining and Calling a Function
Function starts with the keyword function, followed by the name of the function you want to create in JavaScript.
Syntax
The basic syntax for declaring a function are mention below:
function functionName() {
// Code to be executed
}
The simple example of a function, that will show a hello message as shown below:
// Defining function
function sayHello() {
alert("Hello, welcome to this website!");
}
// Calling function
sayHello(); // 0utputs: Hello, welcome to this website!
Adding Parameters to Functions
Specifying the parameters is use to define a function that can accept the input value at run time.
The parameter works as a placeholder variable in a function; they are replaced by the values at run time.
Parameters are set on the first line of the function inside the set as shown:
function functionName(parameter1, parameter2, parameter3) {
// Code to be executed
}
Stated below example will accept two numbers as an argument, add them and display the results through displays() function.
// Defining function
function displaySum(num1, num2) {
var total = num1 + num2;
alert(total);
}
// Calling function
displaySum(6, 20); // 0utputs: 26
displaySum(-5, 17); // 0utputs: 12
Default Values for Function Parameters ES6
With the help of ES6, you can now specify the default values to the function parameters. If you have no arguments that are provided to function this is called default parameter values will be used. As shown in Example and very useful in JavaScript.
function sayHello(name = 'Guest') {
alert('Hello, ' + name);
}
sayHello(); // 0utputs: Hello, Guest
sayHello('John'); // 0utputs: Hello, John
Returning Values from a Function
Using the result statement a function can return a value back to the JavaScript which is called function. Return statement mostly used/placed at the last line before closing the bracket and ends with a semicolon, as shown in the example.
// Defining function
function getSum(num1, num2) {
var total = num1 + num2;
return total;
}
// Displaying returned value
alert(getSum(6, 20)); // 0utputs: 26
alert(getSum(-5, 17)); // 0utputs: 12
Working with Function Expressions
One can call / display a function, you can either use function expression or function declaration syntax.
// Function Declaration
function getSum(num1, num2) {
var total = num1 + num2;
return total;
}
// Function Expression
var getSum = function(num1, num2) {
var total = num1 + num2;
return total;
};
- 5 years ago
- Zaid Bin Khalid
- 8,833 Views
-
9