Why do I need to use `return`?

please refer to this.

The 'return' keyword is powerful because it allows functions to produce an 'output'. We can then save the *output* to a *variable* for later use."

It says there that the 'return' keyword is capable of producing an 'output' that can be saved into a 'variable' for later use. It didn’t tell us how to set an identifier(Variable name) to that 'variable'.that was created by the 'return' keyword.

function monitorCount(rows,columns) {

return rows*columns

}

const numOfMonitors = monitorCount(5,4);

console.log(numOfMonitors);

I believe that the purpose of the code above is to show us how we can set an identifier to the variable that was created by the return keyword.

please refer to this. " The return keyword is powerful because it allows functions to produce an output. We can then save the output to a variable for later use."

It says there that the ‘return’ keyword is capable of producing an output that can be saved into a variable for later use. It doesn’t say how we are going to set an identifier/variable-name to that variable.

function monitorCount(rows,columns) {

return rows*columns

}

const numOfMonitors = monitorCount(5,4);

console.log(numOfMonitors);

I believe that the purpose of the code above is to show us how we can set an identifier to the variable that was created by the return keyword.

by doing this —> let numOfMonitors = monitorCount(5,4) we are basically saying that the output of the return keyword will be saved inside the variable called numOfMonitors. We can also interpret it as saying that the variable called numOfMonitors will have the same value as the value of return output , because monitorCount(5,4) is equal to 'return output' .

Sorry for my terrible explanation, I hope I make sense.

Hi

I tried the same code block created a variable inside the function however, I get the output as 20 but, I also get undefined However, if the variable is created outside the function code block then the output is 20 I don’t see undefined.

Yes! I believe that’s because when you declare a variable outside of the function, it is a global variable, meaning you can use it anywhere, as opposed to declaring a local variable (which only exists inside the function) that can only be used within the function, and thus, if used anywhere else, outputs an “undefined”.

Hope this helped :grin:!

1 Like

Thank you :slight_smile:

Of course!! Happy to help :wink:

function monitorCount(rows, columns){
  return rows * columns;
  const numOfMonitors = monitorCount(5,4);
}
console.log(numOfMonitors);

and I am stuck on the last i
4. To check that the function worked properly, log numOfMonitors to the console.
I get confused on this lesson.

function monitorCount(rows, columns){
  return rows * columns;
}
  const numOfMonitors = monitorCount(5,4);
console.log(numOfMonitors);

but then i try to do this :slight_smile:

Why on this situation const not working inside function?

Hello @blogsolver13561 and welcome to the Codecademy Forums!

Remember that once a return statement is executed, the function is exited and any code following the return statement will not be executed. See more on the return statement here.

This is my answer;

const numOfMonitor = monitorCount;

function monitorCount (rows, colums){

  return rows * colums;

}

console.log(numOfMonitor(5,4));

Worked like this because of the hoisting.

const numOfMonitors = monitorCount(5,4);
function monitorCount(rows, columns) {
   return rows * columns;
  }
console.log(numOfMonitors);

Adding the variable inside the function will block that variable to work only inside that function. What the variable does here is just storing storing the multiplication result and that’s it. You can’t use the variable for anything else here, so what was the scope of adding the variable inside the function?

To take advantage of the variable, and this is why we are creating it here, is to use it later with the value inside it for other purposes. If it’s outside of the function, we can use it elsewhere. Otherwise it just lives inside the function and has no other purposes. You don’t even have to use it if you don’t want to store the result and use it later for something else.

I hope it clarifies your doubts :slight_smile:

1 Like

Hi @tosasilviu,

Thanks for your reply :slight_smile:

My original posts were over a year ago, and I now understand the issues related to scope, and the reasons for assigning the results of computations to variables for later use, both of which you mention in your explanation, and which had initially confused me when I was still a beginner. I’m sure your explanation will be helpful to others who are struggling with these concepts, and who have similar questions to the ones I had :+1: