A function is a block of code or a set of statements which has a name and performs some task. A simple example is a function that displays a message at the console or a function that performs addition of two numbers.
A function can be called as many times as required. It may or may not return a value.
Benefits of Function
Following are the important advantages of using functions.
Promotes code reusability. When the same set of statements is required at many different places, then instead of repeating those lines, they are placed in a function, thus promoting code reusability.
Enhances modularity. Separating large code into function blocks makes the code modular.
Makes maintenance easier. Placing statements in a function also makes code maintenance easier since if there is a change, it needs to be done at a single place only. If the same code was repeated at multiple places, the change would be required at every place.
Enhances code readability. Functions make the code cleaner, organized and easier to understand since writing all the code at one place makes makes it very difficult to understand and modify.
Function Syntax
A function is declared using function keyword followed by a user defined function name. This name is followed by parenthesis with optional arguments(discussed later) as shown below.

function functionName() {
   // function code
}

A function written in above manner is called function definition.
Function Expression
A function defined in above manner using function keyword and a function name is also called Function Declaration. A function can also be defined using a Function Expression. A function defined using function expression has almost the same syntax as function declaration but a function expression assigns the function definition to a variable.
Thus, a function expression for the above function will be

const f = function functionName() {
   // function code
}

A function defined using a function expression has an advantage that its name can be omitted from the definition in which case it becomes an anonymous function. Example,

const f = function() {
   // function code
}
Calling a function
Function definition can not execute on its own, it is just like a piece of code which is not used. In order to execute a function, it needs to be called. A function is called using its name followed by parenthesis. Parenthesis should contain arguments only if the function definition expects arguments. Thus, a function should be called using below syntax.

functionName();

Example
Below is a function which displays a welcome message on the console. The function is named displayMessage.

// defining a function
function displayMessage() {
   console.log('Welcome to javascript!!!');
}

// calling function
displayMessage();

Function with Arguments
A function can accept input values. These input values are called function arguments and make a function more dynamic and flexible. Consider the above function that displays a message.
Now instead of writing this message inside the function, we can supply the message as an argument to this function and display it on the screen. This way the function can be used to display any text and not just the constant message.
 There is no limit to the number of arguments supplied to a function. 

Syntax

function functionName(argument1, argument 2) {
   // function code
}

Arguments supplied to a function are scoped to the function only and are not visible outside the function. A function that accepts arguments is called in the same way as a function without arguments supplying appropriate number of values as the number of arguments. Example,

// defining a one argument function
function displayMessage(message) {
   console.log(message);
}

// calling function with argument
displayMessage('This will be displayed');

Arguments are only required when the function should receive some values.
Returning values from Function
A function may or may not return a value. If it should return a value, then this is done using return keyword followed by the value to be returned. return keyword, if present, should be the last statement in function definition. You need not declare the type of value returned from a function as required in other languages such as java where a function needs to declare the type of value that it will return.
Value returned from a function can be assigned to a variable.
Example,

// function definition with 2 arguments
function add(numOne, numTwo) {
  let sum = numOne + numTwo;
  // return value from function
  return sum;
}

// assign value returned from function
let result = add(2, 3);
console.log(result);  // displays 5

Inbuilt javascript functions
Javascript has many builtin functions. Examples of such functions are
alert
setTimeout
setInterval
clearInterval and many more…

Leave a Reply