DEV Community

Cover image for What are functions???
Pedro Alarcon
Pedro Alarcon

Posted on • Edited on

What are functions???

Functions??? What are they exactly... the simple explanation is functions are blocks of reusable code that can be called and executed whenever they are needed. Functions must be given a proper set of instructions so they can be used and most importantly a name of what this function will do. Given the type of code you are writing it will be important. Lets get even more simple, functions is to programming the way recipes is to cooking. Just like a recipe tells you how to cook a specific dish, a function will tell the computer how to perform a specific task.

How do i create a function?

Creating a function is actually quite simple. To create a function in JavaScript all you have to do is use the function keyword-> followed by the name of what this function will do-> parentheses for parameters (Parameters are variables that are defined in the function declaration and are used to receive input values when the function is called)-> the return statement is used to specify the value that the function should output-> and curly braces {} to enclose the function body. The example below is a function declaration that adds 2 numbers together.

Image description

Now what if we want to test that this function is working properly? Well all we have to do is console.log it. The whole purpose of this function is to add a + b. In order to do this look below at the example.

Image description

In this example we used console.log(addNumbers(21, 4)); to get the result of adding a + b. We could use any two numbers not just 21 and 4. Console.log allows us to print display information in the console of a web browser or a Node.js environment. It's a very helpful tool for debugging and understanding how our code is working. Something to get used to because its a tool that will be used a lot. In the example above the result (25) would be displayed in the console bar, By using console.log we can verify that our function is working as expected and see the output of our calculations.

Why are functions so important?

To start off functions are a huge part of writing code and to break it down even more Functions help break down really big and complex programs into smaller, more manageable pieces of code. By dividing our code into functions, we can organize and structure our program's logic, making it easier to read and maintain. Well-named functions with clear purposes can make our code more easy to read and make them self-explanatory. This is important because best believe that depending on the type of code you are writing for your program it will make it a whole lot easier to navigate through it the more neat and in order your code is. A major tip is to never write a huge function that could confuse you, in fact by dividing our code into smaller functions, each function can have a specific responsibility or task, making it easier to read and maintain. Remember the more organized the better!

Top comments (0)