Why is return necessary?

Hi, guys!
I have a noob question here: Why does it print ‘undefined’ instead of ‘35’? Why is the ‘return’ needed?

function rectangleArea(width, height) {
let area = width * height
}
console.log(rectangleArea(5, 7)) // Prints undefined

As the theory of the lesson says, the computer did calculated the area but it is stored in memory, so we need to get it with the keyword Return, if you don’t, it says undefinded.

1 Like

Occasionally I find some of these exercises confuse the ■■■■ out of me and then go on to find out that it was the exercise that’s not great, and not me. So, replying to the OP, yes, it does not describe why ‘undefined’ is printed:

function rectangleArea(width, height) {
let area = width * height
}
console.log(rectangleArea(5, 7)) // Prints undefined

You say that is “stores it to memory” but you don’t say why it is not printed and I have no idea.

What’s more, the actual exercise takes a completely different shape to the example.

Still none the wiser and don’t completely get why we need ‘return’.

taking a stab at an explanation ,even though I am a noob myself! :

There is nothing within the statement (which starts with the function keyword, and ends with the 2nd } /squiggly bracket ) that tells the computer it should give us something back. So, when the .log method calls the function, it only returns the default value “undefined”, since there is nothing in the code statement to tell it to do differently.

Is that helpful at all? I feel like I am starting to grasp this, but I am not yet in a position to explain it clearly.

-Nick

2 Likes