What is return?

function monitorCount(rows, columns){

return rows * columns
}

const numOfMonitors = monitorCount(5, 4)

console.log(numOfMonitors);

What is return??? It is really getting confusing…please someone give me a good answer please…last week I posted a question and NONE REPLIED!! Please help me in this question please…I just want to know that what is return and it’s purpose… :tired_face: :tired_face: :weary: :sneezing_face: :sneezing_face: :sob: :sob:

The return statement returns a value (like a number, string, variable, object, and more…) back to the call to the function. This is useful when we want to obtain, store, and/or use a value from a function.


Breakdown

function monitorCount(rows, columns) {
  return rows * columns;
}

This function, named monitorCount has two parameters: rows and columns. The function returns the product of rows * columns when it is called.

If I call the function using

monitorCount(5, 4);

this executes the function with 5 being the argument for rows and 4 being the argument for columns. Then, we reach the return statement. rows * columns is calculated and this value, 20, is passed back to the function call.

Now that the value has been passed back to the function call, I can store this value in a variable, use it in a calculation, or do one of an endless possibility of actions.


Examples
Here, numOfMonitors stores the value returned from the function and passed back to the function call, which is 20.

let numOfMonitors = monitorCount(5, 4);
console.log(numOfMonitors); // prints 20

Here, I use the value returned from the function in a calculation with mathematical operators.

let halfNumMonitors = monitorCount(5, 4) / 2;
console.log(halfNumMonitors); // prints 10

There are countless other ways a returned value can be used. See the documentation for return here.

1 Like

Thank you very much… :smiling_face_with_three_hearts: :kissing: :grinning:

1 Like