JS FUNCTION AND ITS TYPES
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.
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
• 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.
PARTS OF A FUNCTION
USUALLY, A FUNCTION HAS 2 PARTS
• Function Definition :
function functionName(Parameter value’s separated by comma)
• 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.
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:
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