Why do I need to use `return`?

Thank you @mtf.
So, to clarify your example.

I know there is more to this, but in basic terms:

let number = 5; variable is outside of the { } so it is global scope

function calc(){
return number;
}

console.log(calc());

function calc() {
let number = 5; variable is inside the { }— local scope
return number;
}

console.log(calc());

1 Like

That is correct. In the second example above, number is not accessible from global scope.

3 Likes

This was also my thought. I get the logic of how the code is computed now, but still in limbo as to how each would be more beneficial than another in a practical sense.

1 Like

In terms of the practicalities…

SECOND EXAMPLE

In other words,   return rows * columns    would do the job more concisely and efficiently.

and

However …

FIRST EXAMPLE

In other words, it’s not just logged to the console and then lost.

Once the concept of global and local (block) scope is understood (later on in the course), it starts to become clear why the variable has to be defined outside of the function if we also want to be able to access the computed value, which has been assigned to it, from anywhere outside of the function.

1 Like

Isn’t this called hoisting (calling a function before it has been declared), and wasn’t it stated in the course that it isn’t recommended? I’m only a beginner and is seeking clarification from the experts

Hoisting has to do with what items are assigned to namespace memory on the first pass through the code by the parsing engine. Declared functions and variables are hoisted to the top of their scope. Function expressions and variable assignments are not hoisted.

Declared functions can be called either before or after they appear in the source. Function expressions cannot be called until after they are encountered in the source.

Scope isn’t mentioned at all prior to this lesson. Without knowledge of scope to preface this lesson it’s definitely not clear as day.

This thread is very helpful. Thanks!!

Super confusing >_< In earlier examples, the lesson showed a much more intuitive way that would have solved this, so what are we trying to accomplish with RETURN?

function monitorCount (rows, columns) {
console.log(rows*columns);
}

monitorCount(5,4)

1 Like

I think this post is exactly what this lesson is missing to properly understand the concept and the WHY of whole “return”. Thanks! :pray:

2 Likes

Codecademy did an excellent job in HTML and CSS, but in JavaScript course, they sometimes made concepts unnecessarily complicated and confusing for beginners. Without saying " When a function is called, the computer will run through the function’s code and evaluate the result of calling the function. By default that resulting value is undefined .", simply “Every function in JavaScript returns undefined unless otherwise specified.” (thanks Brandon Morelli) followed by examples would have been much easier for students to follow.

3 Likes

Thank you very much for taking the time to explain this in such detailed way. It did help me to understand the concept perfectly as well as to understand why the previous lesson was a bit confusing with the current one.

2 Likes

wow, thanks for the explanations and all the effort put into answering this tricky question. have been confused about this issue for a while. now i really need to find a way to save this answer so that i can re-read it alone the way. i think i sort of understand what you mean at this stage. maybe i will know it more thoroughly when i am further down the road.

You’re welcome, @text9028884487.

You can save this HTML page on your computer with, Save Page As. Create a folder in your documents root called, Saved Pages and save it there or in a discuss folder. You can then click the file and open it in your browser, offline.

2 Likes

This might be a stupid question but I’m very confused.
I don’t understand why
'When a function is called, the computer will run through the function’s code and evaluate the result of calling the function. By default that resulting value is undefined . ’

When in the previous segment that teaches how to call a function, it did print on the right hand panel when called and not as ‘undefined’… I assumed that meant the function was called?

‘However, a function declaration does not ask the code inside the function body to run, it just declares the existence of the function. The code inside a function body runs, or executes , only when the function is called .’

Hope this makes sense …

1 Like

legend reply! Thank you so much for the insight!

2 Likes

I agree with shoquim (thank you for all advice posted).

I’m a complete beginner and this exercise was extremely frustrating in the manner in which it was worded step-by-step. If anyone reads this from the Codecademy team, please consider a updating to a clearer guide for this exercise.

In case it may help anyone, I was able to return answer of 20 for numOfMonitors with the following:

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

Best wishes to everyone in coding

ques
I don’t get why do we even need to assign a variable while this works?

  • what is the purpose of return ?

To give the result back to the caller where it might be needed for further use. Generally we do not print in a function but rather give back the result and let the caller print it/use it/assign it.

so you saying that we don’t use “console.log” to print the result but rather calling the function , but in calling it the data disappears- as said in the lesson- and to catch them we use return?

If we don’t return anything there won’t be any data for the caller to use.