Functions By Chloe

Function is a reserved keyword in the Javascript language, meaning it can not be used for anything else other than declaring a function.
Functions are a core concept in any programming language, they are a block of code that perform an action or returns a value.
Functions can be called upon at any point throughout the code.
Here are some definitions that you should be aware of before continuing:

FUNCTION - A block of code that performs an action or returns a value.

PARAMETER - The values that a function can accept. These are optional.

FUNCTION EXPRESSION - Created by assigning a function to a variable.

ANONYMOUS FUNCTION - A function without a name. Mainly used with function expressions and arrow functions.

ARROW FUNCTION - Represented by =>. They are an easy more concise way of writing functions. Arrow functions are always anonymous functions and a type function expression.

Declaring Functions

To declare a function, you use the following syntax:
            function functionName(parameter){
                    //code to be executed
            }
        
The functionName should give the developer a clue as to what the function does, so that other developers can easily read the code.
The parameters are optional but can also have multiple arguments, they need to be seperated by commas. (a, b).

Below are three examples one without a paramter, one with one argument in the parameter and the other with multiple arguments in the parameter.

No arguments:
                function hello(){
                console.log(Hi, how are you?);
                }
                
One argument:
        function number(a){
            return a;
        }
        
Multiple arguments:
        function add(a, b){
            return a+b;
        }
        
Javascript will not do anything with these functions until they have been invoked. Which happens when you call the function name followed by a ;

hello(); => 'Hi, how are you?'

number(29); => 29

add(7, 5); => 12.

Function Expressions

This is when a function is assigned to a variable. When using the function expression you can remove the function name - making it an anonymous function - this makes the code more concise.

The first example is a function expression with the function name still included, the second example is then the function expression with the function name omitted.

As with all functions, Javascript will not do anything with the function until it is invoked, which is done by calling the variabe name.

With the function name:
        var sum = function add(a, b){
            return a+b;
        }
        
Without the function name:
        var sum = function(a, b){
            return a+b;
        }
        
To call upon the function expression you would do:
        sum(2, 5) => 7
        

Arrow Functions

Arrow functions are a newer method in javascript they are always anonymous functions and a type of function expression. They are a way to make the code even more concise.

We replace the function keyword with =>, still assigning it to a variable.
We would write the above example as an arrow function:
        var sum = (a, b) => {
            return a+b;
        }
        

If there is only one argument in the parameter the () can be removed:
        var num = x => {
            return x;
        }
        
If there is no parameter included, you must include the empty ():
        var hello = () => {
        console.log("Hi there!");
        }
        
As the above example are only a one line return, they can be reduced down even further by removing the return keyword and curly brackets:
        var sum = (a, b) => a+b;
        
How you write arrow functions is personal preference as all of them will give the same result.