Understanding Javascript Function

Understanding Javascript Function

As you dive deeper into the Javascript programming language, there are some things you should be familiar with, part of which are Javascript functions. You may need to perform a specific operation while making use of some functions.

This article will take you through how to declare, invoke, and return a function.

Functions are part of the fundamental building blocks in JavaScript. Functions are a set of statements that perform a task to calculate or return a value. A few things to take hold of when declaring a function:

1)How to call a function: we need to add the name of the function () and the parenthesis, with a semi-colon to show that it is a statement.

2) Functions can have inputs when declaring them. These inputs may change the behavior of our functions.

Declaration of Function

In Javascript, functions are declared in this order using the following syntax: Function KEYWORD, Function NAME, and Parentheses(). Remember that names can be a combination of letters, underscores, digits, and dollar signs. The parentheses may include different parameters when declaring a function. One or more parameters can be used in a function declaration, such as (Parameter 1, Parameter 2, parameter 3).

Function Parameter

The function parameters are names that are inside the parentheses in the function definition. In javascript, functions called without a parameter is undefined. Therefore, a value must be assigned to a parameter.

example:


 function ourFunction(parameter1 , parameter2) {
  console.log(parameter1 + parameter2);
 }

OUTPUT:

undefined.JPG

As said earlier, when the value of a parameter is not assigned, it displays undefined

Here is how to declare a function with a value.

function greet(Peter){
   console.log('Hello' + Peter)
 }
  greet('Peter');

OUTPUT:

Hellopeter 2.JPG The output looks untidy lets concatenate to make it more meaningful.

function greet(Peter){
     console.log('Hello' + '  ' + Peter)
 }
     greet('Peter');

concatenated peter.JPG

Function Invocation

Function invocation occurs when code written inside the function executes; that is, when the function is called or invoked.

Example:

Lets create a function to find the products of (x , y). We set our function name, as ourProduct, followed by two parameter x and y, and its value (2, 3).

See for yourself.

function ourProduct(x,y){

    console.log (2 * 3);
 }

OUTPUT:

nothing.JPG

It displays nothing because our function is not invoked, lets now call the function name and pass in values as arguments see what it displays in our console.

 function ourProduct(x,y){
    console.log (x * y);
 }
  ourProduct(2,3);

OUTPUT: invoked output.JPG In a nutshell, function invocation allows the function to be executed when it is invoked.

How a Function Returns a Value

To return the value of a function, you need to add a return statement and the exact value to be returned. This allows the browser to understand that the function statement to be printed is being specified.

var x = findProduct(4, 3);      // Function is called here, and the return value will end up in x

function findProduct(a, b) {
    return a * b;              // Function returns the product of a and b
}

For a function to return a value, it needs an argument. The argument will be used to calculate the value( the return function is part of the argument; it is used to end a function execution).

Note that; We use console.log in the examples above. This is because we want the function to be logged or printed on our console. But the return keyword, on the other hand, is used to interrupt the execution of a function and return its value.

Conclusion

Now that you have clearly understood what a function is and how to invoke or return it, I hope you constantly put it to practice to help speed up your learning capabilities.