Tech Speak: Declaring a Function
Tags: javascript, beginners, functions
Published: 2018-11-08 19:51:27

---
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xq7yvbmxahlj5btjdbbq.jpg
title: Tech Speak: Declaring a Function
published: true
description: Diving into the basics of declaring a function    
tags: #javascript #beginners #functions
<meta prefix="og: http://ogp.me/ns#" property="og:image" content="{link to image address}" />
---
How do you make a program do something for you? You declare a function and then later invoke that function. So how do you declare a function? 

There are three different ways to declare a function. A <em>Function Declaration</em>, <em>Function Expression</em> and an <em>Arrow Function Expression</em>. Let's take a look at each of these...

<strong>Function Declaration:</strong>

Using a <em>function declaration</em> defines a named function and is created using the <em>function</em> keyword followed by the name of the function. 

    function foo (parameter) {
        //statements that handle the parameter(s)
    };

When declaring a function using a <em>function declaration</em> the function is hoisted. Since the function is hoisted, it can be called before the function is defined.

<strong>Function Expression:</strong> 

Using a <em>function expression</em> defines a named or anonymous function by setting the anonymous function equal to a variable. 

    let foo = function (parameter) {
    //statements that handle the parameter(s)
    };

When a function is declared using a <em>function expression</em> the function is not hoisted and cannot be called prior to the function being declared. 

<strong>Arrow Function Expression:</strong>

An <em>arrow function expression</em> is defined using a shorthand syntax that does not use the <em>function</em> keyword. Instead it uses arrow notation (=>). However, the function is still set equal to a variable. 

    let foo = (parameter) => {
    //statements that handle the parameter(s)
    }

When a function is declared using an <em>arrow function expression</em> the function is again not hoisted and cannot be called before it is defined. Another important thing to note is that arrow functions do not create their own <em>this</em> value. Do not let the <em>this</em> keyword get you bogged down though. 

Now that we have added terms like parameter and talked about calling a function lets look at these a bit more...

<strong>Parameter(s):</strong>

Functions are like ATMs and parameters are like a debit card. You must provide a debit card for an ATM to handle your request for a withdrawal, and functions must be provided with a parameter to return a result. A parameter is the variable that you are passing through the function and is defined inside of the parentheses when declaring the function. When passing a parameter through a function you can pass more than one, you can actually pass up to 255 parameters through a function. To do this the parameters are separated by a comma. Let's look at what parameters look like...

    const add = function (num1, num2) {
       let sum = num1 + num2;
       return sum;
    };

In the above function, num1 and num2 are the parameters that we are passing through our function that adds two numbers together. Before we move into calling a function let's look at yet another new keyword that was added <em>return</em>. 

<strong>Return:</strong>

Every function must include the keyword <em>return</em> so that the a value is provided from the function. If <em>return</em> is not used the function will always evaluate to undefined. Let's use the function expression from above...

    const add = function (num1, num2) {
       let sum = num1 + num2;
    };

    add(3, 4);
    //this function will evaluate to undefined

     const add = function (num1, num2) {
       let sum = num1 + num2;
       return sum;
    };

    add(3, 4);
    //this function will evaluate to 7 as expected

This may not seem very important, but if your function is for a calculator app not having the return keyword in your functions would cause very big problems for your user since the wrong value or no value at all could be output. One last thing to note is that the <em>return</em> keyword stops the function immediately and does not pass that line of code. 

<strong>Calling a Function:</strong>

Calling a function, also known as invoking the function, is how the function is executed. To invoke a function you reference the function name followed by an opening and closing parentheses "()". Inside of the the parentheses following the function name is where you pass through the arguments that fulfill the parameters of the function. If the function does not take in any parameters the parentheses will remain empty. 

And that is how a function is declared and invoked. 

Until next time :)