A function is the most basic feature of any programming language. It adds the flavor of reusability within the code. Like you are finding the maximum number from an array whenever a new value is added to the array, instead of repeating this chunk of code (for finding the maximum).
We can create a function and write this chunk of code in that function, which will receive the array and will return the maximum.
So, whenever a new value is added, we just need to call this function and it will give us the maximum. It will also decrease the length of the code.
Function Types:
There are mainly two types of functions:
- User-defined functions
- Built-in functions (javascript provides a wide range of built-in functions which make it more useful, time-saving, and productive)
User-Defined Functions:
These functions are added/written by users, e.g developers depending upon their needs. In javascript, the functions are written as:
Syntax
function functionName(parameter1, parameter2, parameter3, …)
{
// lines to be executed when the function is called
}
Here a function is a keyword, and every function will be created by using this keyword then the name of the function will be written in place of functionName and the parameters will be written within the parentheses ()
.
A function can have no or many parameters. In case of no parameter, we will let the parentheses as it is. And in case of more parameters we will write them, comma separated.
Example #01
Consider a simple function that is printing a hello message and has no arguments (or parameters).
function hello()
{
console.log(“Hello, Welcome to AlgoIdeas!”);
}
hello();
- Line#1: Function is created with the name
'hello'
and the keyword'function'
, the empty parentheses()
show that it has no parameters. - Line#2: The function scope starts.
- Line#3: An output is generated as a string and the semi-colon ends the line.
- Line#4: The function scope ends.
- Line#5: The function named
'hello'
is calledhello()
and the semi-colon ends the line.
Output

Example #02:
Consider the following function:
function addition(num1, num2)
{
return num1+num2;
}
let result = addition(15,35);
console.log(result);
- Line#1: Function is created with the name
'addition'
and the keyword ‘function’ is used for creating function. Bothnum1
andnum2
are parameters. - Line#2: The function scope starts.
- Line#3: The return statement is returning the sum of
num1
andnum2
(which isnum1+num2
) to the line from where the function was called (i.e Line#5) and the semi-colon ends the line - Line#4: The function scope ends.
- Line#5: The function named ’addition’ is called as
addition(15,35)
where 15 and 35 are passed to function as parameters, are will be received in the function as num1 and num2 respectively. The returning value from the function is stored in the variable ‘result’. The semi-colon ends the line. - Line#6: The value stored in the variable result is displayed by using
console.log()
. And semi-colon is used for ending the line as a terminator.
Output
