DEV Community

Deborah Kurz
Deborah Kurz

Posted on

The Quick Guide To Writing A Function For Beginners

As beginners to the world of JavaScript and Software Engineering, Functions are often one of the first things we learn how to write.
Below, we will discuss the basic anatomy of a function (the bare bones that act as a framework for building more complex functions).

As a refresher: Functions are something we can make once, then call on many times later in our code. This means we need to build both the function to act as a "container" and put the action we want repeated inside of it.

First let's look at a function's most basic format, then discuss what is on each line:

function nameOfFunction (valuesGoHere) {
return (values, strings, or interpolation);
}

Writing the first line of our function:
First we use the function keyword...
Next we choose a descriptive name for our function...
We'll follow this with () parenthesis. The values we put inside the parenthesis are determined by what kind of function we are making, and what we want our function to do.
We'll end this line with a { opening curly bracket.

Writing the "middle" line(s) of our function:
This is where we will put the code that will be used when we call the function later.
First, we will either use the keyword console.log(); (for our own developer use) or return(); (to have the function return something).
Inside the () parenthesis, we will now put a string or value or even use interpolation.

Writing the last line of our function:
Lastly, to fully contain the function, we must end with a } closing curly bracket.

This framework is the basic building block on which more complex functions can be built.

Top comments (0)