DEV Community

Cover image for Calling a Function
Luis Linares
Luis Linares

Posted on

Calling a Function

Functions are blocks of code that you save to be (re)used later; that specific fragment of code has a predefined purpose and it does a defined task. A function could receive some or no parameters to be able to execute and must always return something.

For example a function that calculates the area of a square would be something like this:

function calculateArea(length, edge) {
    return length * edge;
}
Enter fullscreen mode Exit fullscreen mode

If you want to use that function to calculate the area of a square with

length = 3
edge = 6
Enter fullscreen mode Exit fullscreen mode

You need to do something like this:

let squareArea = calculateArea(3,6);
Enter fullscreen mode Exit fullscreen mode

The value of squareArea will be set to 18.

Top comments (0)