What are JavaScript functions?

Functions are blocks of code designed to perform specific tasks. Functions have one major advantage which is code reuse. This ensures that you write some blocks of code once and use it several times in your program. 

In some programming languages like Java, Functions are known as Methods.

This concept also follows the infamous programming concept known as DRY meaning Don’t Repeat Yourself.

A JavaScript function can be reused as many times as possible — even with different arguments (values), this when done, will produce different results. JavaScript functions are executed when they are called/invoked.


How to declare functions in JavaScript

JavaScript functions are defined by using the function keyword, followed by an identifier/name for that function (it is advisable to use a name that describes what your function does). The example below shows a function that does not print anything (just a sample):

function sayMyName() {
    // some code goes here
}

Now that you have seen how to create a function, you can decide to make your function accept values or not.
Let us take a look at a function that takes in two values (also called Arguments by some authors) and then display to us the sum of those two values.

function addNumbers(var num1, var num2) {
    var sum = num1 + num2;
    console.log(sum);
}

What the code snippet above does is to take in two values (variables/arguments) and calculate their sum before displaying it to the console.

How about taking a look at another method that does not take in any value but displays anything to the console?

function showSomething() {
    console.log(“Hello and what’s up? How are you doing today?”);
}

That is very simple, right?
Now permit me to ask you a very simple question as a quick test for you:

Question: Write a JavaScript function that takes in three values and displays/returns their product to the screen.

I believe you should be able to do that. It is so very simple. I’ll love to see you try. 

Drop your answer in the comment box or if you have any question, you can do well to ask too.

How to invoke/make a call to JavaScript functions

JavaScript functions are executed only when they are invoked just like I stated earlier. I am very sure you will have this question on your mind: 

How am I going to call a method? Does it have a phone number?

My answer to you: Of course not! JavaScript doesn’t have a phone number but you have given birth to a child and you have to make that child fit into the world. That is a call to service.

To execute your function, you need to write in your source code ( some authors call it Source file or Raw code ). Let us see an example where I declare a function and then call it later in the same source file.

function sayname() {

    console.log(“Hello! My name is JavaScript!”);

}

// later in the source code

sayname();

You see that?

In the snippet of code above, where I typed sayname();  is where the function created earlier is called. It is expected that I have Hello! My name is JavaScript as output.
You get that too? If you do get that, then I’m happy to tell you that you are getting there. 🙂

Advertisements