Functions in JavaScript are part of a program. When we are programming it is possible to separate our code based on the specific function they do. Functions are very useful because they help us to better organize our code. These subprograms can be called externally and internally (internally in case of recursion, recursion will not be cover here).

Through functions we can pass values as arguments or parameter, and also a function can return values in JavaScript the reserved word function is used to declare a function or to indicate that is function has been created. Below is an example of how we create and use functions in JavaScript

    //Declare a function
    function name(parameters or arguments){
        Statements to be executed
    Ejemplo 1

    Function sum(){
        var number1 = 2;
        var number2 = 4;
        var result = number1 + number2;

        document.write("The result is " + result);

The above function is a function with no arguments or parameters that declares 3 variables, number1, number2, result. This function adds the two number contained in the variables number1 and number2 they are added and then the result of this sum is assigned to the variable result, and then the result is printed out on the HTML document.

    Ejemplo 2

    function sum(number1, number2){
        return number1 + number2

The difference between Example 1 and Example 2 is that the values for number1 and number2 are received as arguments and the result is return through the same function.

Note a function can receive or not arguments or parameter and can return or not a value