• Register
499 points
3

How to call a javascript function that has return type

 

What is JavaScript Function:

A javascript function more or like other programming functions. A JavaScript function is defined with the keyword, followed by a name, followed by parentheses ().First of the most important is to know that we are having many ways to declare a function or define a function and this is the traditional way to  define function definition.

Function names can contain letters, digits, underscores, and dollar signs (same rules as variables nomenclature). This also follows the same rules as for variables.

We can pass parameters in these curly brackets that we wanna pass in the function:
(parameter1, parameter2,parameter,parameter 4.. ...)

The code to be executed or the logic that gets run, by the function, is placed inside curly brackets: {}

Javascript Function Syntax:

function name(parameter1, parameter2, parameter3) {
  // code to be executed
}

Javascript Function Examples:

There are many more examples of this :
This is basic example of functions in javascript.

function numbercreator(number) {
  return Math.floor(Math.random() + number);
}

 

Through parameter, you can pass values into function and execute the login and after the execution, you can also pass back a value. After the execution in the end function can return a value or use simply return to it the function.

Perfect Solution:

Here’s an example of a JavaScript function with a return value:

 

function addition(a, b) {
    // simple example that multiplies two numbers
    var c = a + b;
    return c;
}

 

In this way, you are calling a function on the basis of the result only and more explanation in the next example.

// call function and ignore return value
addition(6, 7);

 

When you need the result of the function then save it in a variable and make it work. you can use this variable to work further on your program.

var c = addition(6, 7);

 

Once the value gets captured in the variable, you can use that in any other function or loop to further process. This is the basic structure of the function and in javascript, this is the most basic way to define functions. Let me know in the feedback if there is an issue and questions you wanna ask.

1 Comment

Please expand on this subject. Your ability to explaining is wonderful.