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.

3 Likes

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:

2 Likes

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:

1 Like

var numOfMonitors actually needed to be declared globally it can only be used within the function scope it was declared. nice one.

So wrong, in fact, that even after you offered the solution, it is hard to understand without a proper explanation of just what on earth “return” is. There is no follow up, no explanation of how we save a variable for later use, etc. From here on it’s all a tangle.

In order to return we have to have gone somewhere. A function is somewhere to send data, have it operated on, and sent back, post operation. The sending back part is the return.

1 Like

In a transactional sense, we refer to the function as the callee; and the expression in the code that invokes it is the caller.

The caller provides the arguments (the data) and the callee does its job with that data (its parameters), then sends resulting data back to the caller.

The term, ‘caller’ is one you will see often. It’s nice to be clear on what it refers to. The interplay of words in techie discussions is confusing enough. Nail this one down.

Thanks for the reply. Remembering all the details may be hard and require practice, but without grasping the theory it’s all an impenetrable labyrinth. This lesson may have worked better if it were preceded by at least some knowledge in Scope, I think. Probably I’ll have to go over it again.

1 Like

Find a bunch of boxes that you can fit into each other. Fit them together but leave them open from above so you can see inside each one from above.

Now place yourself in the innermost box (using one’s imagination, of course). If every box had two way mirrors, from your vantage point you could see all the way to bicycle on the other side of the garage (pretending we’re doing this in a garage). You could also see everything in all the boxes in between. That’s looking outward.

Anyone inside the very immediate box that your box is sitting in cannot see inside your box. You are invisible. All they see is their reflection. The same goes as we position ourselves in each box on the way out to the garage floor. Even at that vantage point, all that can be seen is a reflection in mirror.

Now remember, we can look outward from all those boxes, else you couldn’t from yours. Each box is a ring around the box it contains. The innermost box is called the lowermost, and once we are free of the outermost box, we reach the uppermost. We know we can only see one way, but everything we can see can communicate back to us, if we call them.

Now consider that each box contains unique objects only found in that box. Those unique objects can be other boxes as well as tangible items. This helps us visualize the containment scenario that best describes scope.

Each one of those boxes is a closure, a namespace, an execution scope. We cannot penetrate the boxes within our present scope, only those which contain our present box. That means we cannot see their variables or enclosed functions. Neither can we penetrate the box that is beside ours. That would mean two way traversal, which is impossible in this particular light, scope.

There is but one direction as alluded to by the two-way mirrors. Up.

The ring of box in box can be viewed as links in a chain. Each link up is one closer to the topmost link. A function at the bottom of this chain can access every link above it. This is called traversing the scope chain. A function buried seven layers down can access variables and functions any number of layers above it, if they are on that chain.

We commonly refer to this virtual object as the scope chain. The topmost link is the global namespace. More one could say if not all matters are cleared up. Pipe in if you have other questions.

1 Like

Once we can envision the idea that lowermost functions can access variables and functions above them just by polling or calling, respectively, it becomes evident that there are threads traversing scopes between one object and another. Any function can call another function so long as it exists on the scope chain. Its arguments travel to a different scope, and the return is the reverse connection, the handshake, so to speak, or bipolarity. It’s a free pass to a foreign scope.

1 Like

That makes perfect sense, thank you so much. I should have clarified, I did know what Scope was (at least in broad terms) since I went through this once, but I feel that these particular lessons are not as well organized or as concise as they could be and I may have to go through them once more. That being said, your explanation was phenomenal and much appreciated.

1 Like

Thanks, the logic for this one makes perfect sense! :face_with_monocle: