JS FUNCTION AND ITS TYPES

Asif Ur Rahman
5 min readSep 21, 2021

WHAT IS A FUNCTION?

In a programming language, a function is a set of guided instructions that we can use later for certain tasks.

In fact, we use the word “function” a lot in our daily life. As we say “today we are going to a wedding function, birthday function or a funeral function”. See that, the functionality of each function here is very different from each other. We stay very happy at the wedding function. But at the funeral, we are quite shocked.

So a special feature of the function is that the functionality of each function is different.

This is as true in our daily lives as it is in a programming language. Some functions calculate numbers to get a sum of the numbers. And some functions is subtracting two numbers. These, of course, will be discussed in more detail later.

WHY WE WILL USE FUNCTION?

If it occurs that the same type of code is repeating in different places in our code file, then we use the function. But this repetition of code is quite different from the loop. Because in the loop condition is used, not in the function. Besides, we get the result obtained from the loop only in one place one after another. But we can use the result obtained from the function wherever we want. So in simple terms, a function is a block of code that we can use anywhere we need.

Usually, if we don’t use the function, we have to type the same code over and over again for the same function which enlarges the source code size. Now Javascript is an interpreted language. It interprets any source code line by line. As a result, the larger the size of the code, the longer the runtime. This increases the code complexity too.

So we can say from the above discussion.

•A declared function is “saved for later use”, and will be executed later when it is invoked

(called).

• Piece of code that operates when called. It is similar to a loop but the only difference is a function has no conditions.

• Function does help with the frequently used code by calling it when necessary thus reducing program size and execution time.

• In JavaScript, functions are objects, and they have both properties and methods. That means we case use a function as a parameter of another function too.

PARTS OF A FUNCTION

USUALLY, A FUNCTION HAS 2 PARTS

• Function Definition :

It means to declare a function. It means to create a function and to say what it will do. In this case, we have to use the keyword ‘function’. Javascript is a dynamic language, so we don’t have to say the data type of the function. We just have to use this “function” keyword. After that, we have to give a function name. It is like a variable declaration. If you have more than one word, you have to use camelCase. After that, we have to set the parameters. In this case, we can also set one function as a parameter of another function. Then between the two curly brackets, we have to write the code of the function.

function functionName(Parameter value’s separated by comma)

{

expression/statements;

}

• Function Calling :

Where we want to use that function, writing the function name in that position is called function calling.

In this case, we have to write the name of the function exactly. If there are any parameters of the function, they will be written in parenthesis. If there is no parameter, you just have to put an empty parenthesis.

functionName (parameters);

TYPES OF FUNCTION

Based On Return Type:

There are two types of functions depending on how you can get your data through a function.

1. Return Type:

If the function returns a value, it is called a return type function. In this case, just use a return keyword to show output. The advantage of this is that you can set the result from a function to a variable and use it later.

In the given example, we have added two numbers by the function addNumbers(). By using return instead of console.log(), we have able to store the value of the function in a variable “result”.Which will be used later.

2. Void Type :

If the function does not return any value then it is called a void type function. The main way to recognize such a function is that this type of function will not have a return keyword.

BASED ON WHO DECLARED IT :

BASED ON WHO DECLARED IT FUNCTION HAS 2 TYPES

1. Library Function or Language defined function:

Those Function which has been created by javascript programming language or various libraries of JS. Which are also called Built-In Function. We can apply it in our source code directly.

Like :

alert();

prompt();

Number();

setTimeout();

etc

2. User-defined function:

The Function which has been created by us.Like, the function addNumbers() is a user-defined function.

Based On Parameters:

Based on parameters functions are 2 types. Like,

1. Without parameter:

This function has no parameter. Like-

2. With parameter:

This function has one or more than one parameter. Like-

Here the num1 and num2 are two parameters of the addNumbers function.

Asif Ur Rahman

Campus Ambassador at Programming Hero

--

--

Asif Ur Rahman

Hi ! I am Asif. A full time web developer. || Web Developer at OmniGo || MERN Stack Developer