Return a Value from a Function with Return

Return a Value from a Function with Return
0

#1

Tell us what’s happening:

I haven’t had any trouble completing this test, but I honestly can’t seem to understand exactly what the
"return" function does. I’ve been trying to understand the concept of it’s use for a while now, and nothing I’ve found online is helping me truly understand what it’s really doing, and why/when I should use it.

Any clarification is super appreciated. Thanks!

Your code so far

// Example
function minusSeven(num) {
  return num - 7;
}

// Only change code below this line

function timesFive(num){
  return num * 5;
}

timesFive(5);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:


#2

A function is something that accepts an input and produces an output. The function’s input comes from the parameters. The return keyword is what you use to output something from a function.

For example, the timesFive function is a function that accepts a number num as input, and outputs num * 5. If you called timesFive(5), it will evaluate to 25.


#3

Thanks for your help. Your explanation does help me wrap my head around this a bit. My next question I guess is, if I wanted to for instance do a console.log() of the result from some operation using the value of my inputs, why exactly fo I need to use return?

I’m still not clear on exactly what it’s doing within the scope of the function it exists in. Does return make the result of my operation accessible outside of the function or something?


#4

Well, I think I finally found the answer I was seeking online:

“The definition of the return construct is “stop execution of the current function/script and return a value to the line that called it”.”

That totally explains it for me. Again, thanks for your help and time on this.