0 points
Submitted by Daniel Greene
over 9 years

# I'm stuck on the return keyword and I don't understand one thing about Return

Can someone please explain to a T what return is because i have already looked at other replies and they still confuse me

Hello, the `return` statement allows your function to `return`/give a value to the rest of your program. Some examples:

``````var squarea = function(x)
{
console.log(x*x);
};``````

This function would print the square of the passed number into the console. But if you wanted to calculate `cube` with the formula `square*length`, you would have to use return, as the function keeps the result. Well, it prints it to the console, but this doesn’t help you, as you can’t access the console. We have to write a function, which gives you the result, which `returns` the result:

``````var squareb = function(x)
{
return x*x;
};
var cube = function(x)
{
return squareb(x) * x;
};
console.log(cube(10));``````

This works perfectly. However, this might seem to be useless, as you could calculate the cube using the formula `length*length*length`, but there are many other things, which really need to be done in many functions (especially, as “Seperation of concerns“ is a programming concept, just like D.R.Y.).

Do you have any further questions?

I hope it helped. :-)

points
Submitted by boring12345
over 9 years

Ben Milford over 9 years

Thanks for this explanation, it really helped me get my head around the concept!

boring12345 over 9 years

No problem. ;-)

Still a little confused but it makes a lot more sense now. Thanks!

boring12345 over 9 years

You are welcome! :-)

gottit! basically a return allows you to use the value you generated from a certain function anytime down the road in your code. (…please correct me if i’m wrong :P )

Correct. :-)

thanks man really helped me :D

Great! :-)

thx!

You’re welcome! :-)

genius

Finally got it! Thanks!

You are welcome! :-)

fucking complicated or my brain tired

that is 3rd longest piece of code I have ever seen

i still dont get it is this too complicated for 13 year olds

Can you be more precise? Which parts cause problems?

im not understanding when you would use it, why, and how. does that help at all

Okay, so the “why would anyone use this” is the main problem. I’ll try to write a new answer below.

thanks

michael oqropiridze almost 9 years

i can’t understand anything about function’s and returns…. that might be for my english thanls anyway

Ryan Berzinski almost 9 years

thanks this helped me a lot now my head isn’t as much in the clouds ver helpful and straightforward

boring12345 almost 9 years

You are welcome! :-)

QuantumLeap42 over 8 years

Mr lee the syntax is saying i’m wrong but it should be right! help please?

boring12345 over 8 years

Would you mind to share your code?

Mike Borman over 8 years

Well this just makes no fucking sense

Joel.Miles925 over 7 years

Would you just avoid saying the f-word?

Ella Hartman over 7 years

???????????

Christopher Chapman over 7 years

this explanation makes me want to swear.

Henry VanDerwater over 7 years

Dude, wtf? Just post the code; thats all we want. :)

uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh, what?

Ben Winter over 7 years

Christopher Chapman - your comment made me laugh…finding it confusing myself - good to hear it’s not just me!

The reason some people dont post the code is because this is a learning academy not find the answer and finish everything with cheating and saying you did it because when you go to do something you wont have any experience at all.

Return is shorthand for “do the calculation and just give back a number”. The alternative is “do the calculation and save the result as variable x”. Here are two examples, one uses return and other other does not, but they both do exactly the same thing:

Example without return: var area; var cirArea = function(radius) { area = 3.14 * radius * radius; }; cirArea(2); console.log(area);

Both codes will result in exactly the same thing, which is “12.56” appearing in the console. But, without return you need to use an extra variable (area) to hold the answer. With return, you can shorten your code a little and make it cleaner. Hope that helps.

points
Submitted by caver
over 9 years

Heather S over 9 years

greeeen89 over 9 years

Thank you! This helped e understand returns to kinda get a feel of it.

vbcodervb over 9 years

ok, I am also beginer with javascript and I had problems with understanding what “return” means. Based on user “caver” explanation , I understood that “return” acts as unvisible variable. Like this.. return === var name. So “return” is unvisible variable that holds unvisible value

Thank you! I couldn’t understand it seemed like return was irrelevant, but the explanation of the extra variable makes more sense

Stepan K. almost 9 years

Thank you for providing a nice example and explanation

Andreas O. Bø almost 9 years

Okay, so if i am caculating the area of several circles i would just ad more console.log(cirArea(#)) lines and they would automaticaly go through the same calculations? then it would suddenly make sense.

anitaqin almost 9 years

this answer is great! explain it in a easy way! ppl who ask this like me must new to everything, some answer will make us more confused, i like this answer anyway, thx!!!

Ninjawhee over 8 years

Thanks for this answer! So basically, it’s just shortening the code

oh thanks so much ! you helped me while i am stucked ! nice answer and example Thanks you

SarthakKanyal over 7 years

good answer but i still dont understand

Christopher Chapman over 7 years

EthanDebnath over 7 years

oh no im still not getting it i feel like killing myself……

Rideedee over 7 years

i didn’t get it either

Henry VanDerwater over 7 years

This one actually makes sense.

I hope Ethan didn’t kill himself :(

Lene Akira Kiiro almost 7 years

Wow thanks. But I am not sure if my understanding is correct–which is, “return” is used for writing less codes?

// Parameter is a number, and we do math with that parameter var timesTwo = function(number) { return number * 2; };

// Call timesTwo here! var newNumber = timesTwo(69); This is the only part that you have to change console.log(newNumber);

I think this lesson was worded very poorly. It feels like this should have been much easier to grasp but I was looking for a more complicated answer.

points
Submitted by Wes

Indeed. Thank you. Seems stupid to overlook it, as I was also looking beyond the immediate solution.

THANK YOU!!!

THanks lol I forgot what ive learned because i was off for 5 days? THanks man

Vik almost 9 years

Thx-I was befuddled:-(

Koni Segg almost 9 years

Thanks

S1monm almost 9 years

Cheers! I spent 30 mins looking for a clue.

sainath kollipara almost 9 years

thanq and u r right this part is not very clear and i think this part should be more clear!!

Ed Chambers almost 9 years

I replaced line 7, using copy/paste, var newNumber = timesTwo(69) and got this error message: “TypeError: Property ‘log’ of object #

Ed Chambers almost 9 years

OK, I reloaded the page, and then it worked, following advice I spotted elsewhere.

Erin Ozolins almost 9 years

Why 69?

Alex Charters almost 9 years

thank you so much

Anthony Myers over 8 years

Yeah, don’t get me wrong. This is a free course and I appreciate it greatly, but some of these are worded so terribly. Hard to understand what I’m supposed to do sometimes.

Kendon Brawner over 8 years

That was definitely easier than what I thought at first, but it makes sense, you just need to give a value to the function.

tamujudo over 8 years

Thanks. I was having trouble on this one too. I think I was trying too hard.

JGQ15 over 8 years

hahah i see what you did there .. pffft hehe * whisper * 69 haha .. okay .. ill shut up now ._.

Awesome, thanks a lot!

kale770 over 8 years

Thanks this helped alot!

Jacque Donahue over 8 years

Oh my lord, I made this way harder than it was. Thanks for the help.

A_fawkes17 over 8 years

Thank you! I was too!

saeberk over 8 years

Brittany Carver over 8 years

Thanks!!

Enio Hysa over 8 years

Thx

Thank you, I thought I had to make newNumber a function, it was worded quite badly!

this really helped thank you

this helped, thanks!

mordye137 almost 8 years

Thank you so much, had no idea wut to do!

Jen Yang over 7 years

Thank you! :)

SkittlesIV over 7 years

Hey thanks

Karyan Ng over 7 years

Thank you!

Yasmin Maher over 7 years

thank you

Comfy CouchMedia over 7 years

nice work @Wes

Thanks Wes!

So someone tell me if what i am thinking is correct. So the return key takes the answer of a function and injects into the position of the function keyword. I think that is what it does buy im not completely sure.

Thanks! I was looking for a more complicated answer, too.

That’s all I needed !!

this is all i did var timesTwo = function(number) { return number * 2; };

// Call timesTwo here! var newNumber = timesTwo(8) console.log(newNumber);

on line 7 var new number = put the timestwo there simple

points
Submitted by dr.chase
over 8 years

Derek Jones over 8 years

Sweet, thanks!

sindu666 over 7 years

At last now i understood this “return” stuff to an extend- thanks for your help.

hello, so i tried to just copy and paste your answer as it was the same as mine except my “return number * 2;” was indented and neither work. was your example not supposed to work or am i missing something else?

this is the code that i am putting in var timesTwo = function(number) { return number * 2; };

// Call timesTwo here! var newNumber = timesTwo(8) console.log(newNumber);

so what am i missing?

• It’s basically asking you to set the variable along with it’s function, and then find a number that will reduce to a number that will have no decimals. As described in the example (which I had to use myself ) it uses 13 % 3 for an example. This translates to the initial return value ( return number / 4;) Which has already divided 13 by 4.

How the code will look with all the modifications.

`var quarter = function(number)` `{return number / 4;}`

`if (quarter(***Number goes here!***) %3 == 0 console.log("The statement is true");} else { console.log("Thestatement is false");}`

Basically you place a number in between the quarter’s parentheses that can be divided by 4 and then divided about 3 that will leave no decimal.

• I used 48 which divided by 4 ( 48/4 = 12 /3 = 4) no decimals.
points
Submitted by Jessica

bunghoney almost 8 years

Oh my God, THANK YOU. Stared on this for about an hour…

Jena Attia over 7 years

THANKSSS’

#### Practical use of functions

Have a look at this file and click the `Click Me!` button. The source code can be found here. We use a simple function, which is called, after the button is pressed:

``````var alertName = function() {
alert("Nice to meet you, " + name);
};``````

And we have to tell the browser, what to do. And therefore this function is a list of things, which should be done, when the function is called.

#### Practical use of return

Have a look at this file and at the source code. Now we splitted the tasks:

``````var alertName = function() {
var name = getName();
alert("Nice to meet you, " + name);
};

var getName = function() {
};``````

While this seems to be more complicated, `return` allows us to seperate the tasks: Now one function asks for the name, while another `alert()`s the name. In this small example it is not really needed, but imagine you wanted to add another function `reverseString()`, which reverses every string and therefore can be used in many different ways, so `reverseString("Homer")` returns `"remoH"` and `reverseString("Long text")` returns `"txet gnoL"`.

Note: I did not implement it in this example, as you’ll later learn, how to do it (simply use a loop and loop from the end of the string to the beginning of the string). If it helped, I’d also give you further examples using `reverseString()`.

points
Submitted by boring12345

boring12345 almost 9 years

ComputerXplorer almost 9 years

Thank you so much!!

boring12345 almost 9 years

You are welcome! :-)

ComputerXplorer almost 9 years

You deserve even more praise for wasting your time, helping young coders like me! ;)

987tz2 almost 9 years

I still don’t get it i think i am the only one :(

boring12345 almost 9 years

I’m sure you aren’t. :-)

DinaRowe almost 9 years

nooo, you’re not the only one…

Sarah over 8 years

You two arent the only ones too T^T

Henry VanDerwater over 7 years

WTF?

1 vote

In simple statement :-

Return is all about “calculate the steps inside a function, then stop the function process, without printing“ and the returned value of the function can be used anywhere within document.

Ex:-

``````    var foo = function(mango) {
return mango * 5;
}
foo(100);``````

What actually happens is, when you use return, it would execute the return statement, but won’t print right away.

Try to execute the above code without calling the function. What happens? - nothing would be printed! because we haven’t specified to print or show anything, we just used “return” to calculate the step and stop the function, then the function can be called anywhere outside of the function to print as we like.

Try using this code:-

``````    var foo = function(mango) {
console.log(mango * 5);
}
foo(100);``````

If we use console.log, it prints the answer right away, then what’s the use of using function? so we use return to stop the function for a problem, then call the function-parameter outside from the function to print the solution.

Hope it helped you.

Regards, Mithun.

points
Submitted by Mithun

sindu666 over 7 years

thank you

1 vote

i recommend reading jquery first and you will understand the formatt offhead and heart

var timesTwo = function(number) { return number * 2; };

// Call timesTwo here! var newNumber = timesTwo() console.log(newNumber);

points
Submitted by stanley-need-solver
almost 8 years

1 vote

I got a code if you still get confused: var newNumber = console.log(timesTwo(69)); Well, I just gave you a slight headstart…… maybe. =)

points
Submitted by Emma
almost 8 years

lel

have fun with that

1 vote

I also struggled with the return keyword when I did JavaScript. But in PHP they give the following explanation that helped me understand better:

The Return Keyword Instead of printing something to the screen, what if you want to make it the value that the function outputs so it can be used elsewhere in your program? In PHP, the return keyword does just that. It returns to us a value that we can work with. The difference between this and echo or print is that it doesn’t actually display the value.

Think of it like a calculator solving a mathematical problem that takes several steps to complete. The value from each step is computed, but we don’t see the result until we get the final answer. In other words, each value is returned and the final answer is echoed on screen for us.

Hope this helps…

points
Submitted by Luzé
over 7 years

Well, the return basicly means when you’re running a code and you dont want to print alot of stuff out, it’s just like saying, *”Wait,* I dont want this code to run. I can save myself alot of time.” so I hope this helped.

points
Submitted by DragonBallFan100
over 7 years

Hi i’m having some trouble with the lesson.

i feel like i did everything write in the lesson, but the code keeps using the “else” statement rather than the “if”

can someone take a look and help me out

thanks!!

var quarter = function(number) { return quarter/4; }

if (quarter(48) % 3 === 0 ) { console.log(“The statement is true”); } else { console.log(“The statement is false”); }

points
Submitted by ayjaelee
almost 8 years

alexandru.taran almost 8 years

return number/4

Hello, the return statement allows your function to return/give a value to the rest of your programm. Some examples: var squarea = function(x) { console.log(x*x); };

This function would print the square of the passed number into the console. But if you wanted to calculate cube with the formula squarelength, you would have to use return, as the function keeps the result. Well, it prints it to the console, but this doesn’t help you, as you can’t access the console. We have to write a function, which gives you the result, which returns the result: var squareb = function(x) { return xx; }; var cube = function(x) { return squareb(x) * x; }; console.log(cube(10));

points
Submitted by The_Red_Devil
over 8 years

boring12345 over 8 years

Is there a reason why you copied my answer?

// Define quarter here. var quarter = function (number) { return(number / 4)

}

if (quarter(48) % 3 === 0) { console.log(“The statement is true”); } else { console.log(“The statement is false”); }

points
Submitted by Nicolas Schroeder
over 8 years

Michael Okropiridze over 8 years

thanks..but i had those code but still getig error what will changes if i place 3 to 48 … calculation is right 3 modulo 3 equal 0 “statemant is true”…????

Loll Alleliuym almost 8 years

equal: 3,12,48,144 and ie( number multiply 3),

why are we using so many variables

points
Submitted by Gaurav Shahdeo
almost 9 years

RexGalilae over 8 years

this IS java

Nicolas Schroeder over 8 years

exactly

Ryan Berzinski over 8 years

duh

Eminem5465 almost 8 years

Skygate over 7 years

LOL