DEV Community

Cover image for Functions - What are they?
Sachin Pant
Sachin Pant

Posted on

Functions - What are they?

What are Functions?

Functions are those building blocks of a programme which help you do repetitive work. They help save time. They reduce the amount of code. They help save our energy and they might increase the efficiency of our programme.

How do we declare a function?

There are different ways to declare a function. For e.g. -

  1. Regular Binding - const square = function(x) {
    return x*x;
    };

  2. Declaration Notation - function square(x) {return x*x; };

  3. Arrow Function - const square = (x) => {return x*x; };

We use one of the above ways according to our comfort and type of a programme.

What is Call Stack?

Suppose you declared a function and then have to call back later in a programme. After getting a value from it we want to execute our code after the point where we called the function. For that we need to store the point where we called the code in a memory, this is our call stack. It's duty is to remember where in our programme we call a function. It should never fill whole of our memory otherwise it will stop the programme and ask to clear the memory or as it is called in the programme 'blow the stack'.

What is Recursion?

A function that calls itself is called a recursive function. It is okay for a function to call itself as long as it is not overflowing the stack. It allows us to write a function in a different style.

It could be easy for a reader to understand the code written in a recursive format. But it might not always be the case when we talk about code efficiency. As many a times a function written in a loop might be more efficient than a function written in a recursive format.

So it totally depends on the need of the programme whether to write a function in a normal format or a recursive format.

Conclusion

This was the basics of function and we can see that functions are there to make the life of a programmer easy and to help reduce the time and workload of a programme and a programmer.
It is the building blocks of a programmer. Just like cells are a building blocks of lives to a tissue.

This is a blogging challenge from #teamtanayejschallenge

You can visit the website at: https://ejs-challenge.netlify.app

Top comments (0)