Javascript Functions

What is Javascript Function

Javascript functions are building block of code that is only executed when it is called or invoked. Function plays a very important role in building very small to large scale web applications.

Another helpful feature of Javascript functions is that they are reusable. We will understand that in a while.

Javascript Function Syntax

Javascript Function starts with the reserved keyword function. Then the function name with followed by parenthesis (). Finally the warped bracers for the code statements.

function MyFunction() {
    // codes belongs to MyFunction function
    // more codes
}

A javascript function also can have parameters inside the parenthesis for passing value to the function from the outside of the function. Consider the following syntax;

function MyFunction(params1, params2, ....) {
    // codes belong to this function
    // params1, params2 are available
}

Notice that unlike the first function, the second function is parameterized with two parameters that will accept value while calling the function.

In the comment, params1, params2 are available means params1 and params two hold the sent values. So that we can work with those inside the function.

Invoking/Calling Function

Invoking or Calling a function refers to giving command for executing a function. That means when a function is called or invoked the function is executed immediately. In javascript, use the function name with parenthesis. The function will be executed then.

Example-1:

// function starts
function MyFunction() {
    var a = 10;
    var b = 13;
    var sum = a+b;
    console.log(sum);
}
// function ends

//invoking function
MyFunction()

// Output: 23

It is that simple! The variables a, b, sum we used in the function are local variables. That means we can’t use those variables outside of the function.

Example-2: (Parameterized function)

// function starts
function MySum(a, b) {
    var c=11;
    return a + b + c;
}
// function ends


// calling function in console.log
console.log( MySum(10, 20) )
console.log( MySum(5, 10) )
console.log( MySum(10, 10) )
console.log( MySum(11, 11) )

//output: 41
//output: 26
//output: 31
//output: 33

Like that we can pass the different values to a function. Now we will extract some important concepts from the above example.

We have called the function several times with different values that returned us the addition result. Remember that we said functions are reusable.

Exactly! Notice that with a single function we can calculate as many numbers as we want without rewriting the business logic again. In this way, you can create more complex functions and calculate just passing values.

Another thing, we have used return  keyword in the function. return is a reserved keyword in Javascript to return some values from a function to the calling point of the function. Just like the above.

Some things to Remember

  • Use CamelCase for writing function names. It is a convention. It will help you to differentiate variables and functions from large source code.
  • Don’t use any statement after the return statement in functions. Because statements after return key are not going to be executed.

Homework

Really it is not enough! Practice and play with function by writing your own. Now your task is:

  • Write functions for subtracting, multiplying, dividing values.
  • Write a function for calculating temperature Celsius to Fahrenheit.
Posted in