Basic function
Suppose you want to know the result of 2 plus 2. You wrote a few lines of code for this and the result came right. Now again you want to know how much comes in 4 plus 4, so what to do now? Write some line code again. But look here, again and again, you want to find the sum of two digits. Now if something can be arranged for this without having to re-write the code again and again, if I give two numbers in it, it will give me the sum? Yes, that’s the function of function in programming. Today I will talk to you about this function.
You can compare functions to a machine. Here you just give some input and it will give you the output just like you. You have some clothes, you want to make clothes from them. What do you do Maybe you can make it yourself, or most of the time what we do is bring clothes to a store and our body size is kept there. After a certain time, you go and bring your ready-made clothes. Many times you don’t even need to know what Taylor did with your clothes. You just gave him clothes, he made clothes like you.
Take the example of the sum given at the beginning in exactly the same way
Sum_machine (number 1, number 2){
Sum = number 1 + number 2;
Send sum;
}
Show sum_machine (2, 2);
Show sum_machine (4, 4);
We know that in JavaScript everything is an object. So what is the function object? Yes, functions in JavaScript are also objects. This function in JavaScript is called the first-class function. Because you can return functions from functions. Again, you can store the function in variables. If you want, you can pass another function as an argument of one function. I will discuss with examples below. Everything will be clear.
We have already used many built-in functions already.
For example: console.log (‘Something here’)
Get Saim Hasan’s stories in your inbox
Join Medium for free to get updates from this writer.
We have written something inside console.log here and it has been printed on the browser console. Now we don’t have to think about what console dot log does, then how it reached the browser console and printed the text given by me. Because it is also a function. And its job is to print the input given by us in the browser console, diameter. There are many more built-in functions like this that have already come with JavaScript or something else. Even if you just call, it will start working.
Now a situation may arise where our work may not be done with the built-in functions. In that case, we can create our own function. Now let’s talk about how to declare a function. You must use the function keyword to declare a function. Then the name of the function, then start with curly bracket and inside you will get the statement of what you will do and finally, the function will end with curly bracket again.
function funcName (){
// Statements
}
Declaring a function in this way is called a function statement.
Again, this is how we can declare a function
var funcName = function (){
// Statement
}
Here we have assigned an anonymous function to a variable. Declaring a function in this way is called function expression.
Hurrah we can declare a function now !!
Our next chapter will cover how to use a function and also some spicy topics.



