This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Daniel Greene
over 11 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

Answer 50d8dff9f4c9c765510000ac

92 votes
Best answer

Permalink

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 11 years

33 comments

Ben Milford over 11 years

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

boring12345 over 11 years

No problem. ;-)

Maddiexp almost 11 years

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

boring12345 almost 11 years

You are welcome! :-)

Jona almost 11 years

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 )

boring12345 almost 11 years

Correct. :-)

wardbeyens almost 11 years

thanks man really helped me :D

boring12345 almost 11 years

Great! :-)

nugget,biscuit almost 11 years

thx!

boring12345 almost 11 years

You’re welcome! :-)

Gonja Michael almost 11 years

genius

Julia Stuxgren over 10 years

Finally got it! Thanks!

boring12345 over 10 years

You are welcome! :-)

Otabek Otaboyev over 10 years

fucking complicated or my brain tired

???????? over 10 years

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

Bill over 10 years

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

boring12345 over 10 years

Can you be more precise? Which parts cause problems?

Bill over 10 years

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

boring12345 over 10 years

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

Bill over 10 years

thanks

michael oqropiridze over 10 years

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

Ryan Berzinski over 10 years

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

boring12345 over 10 years

You are welcome! :-)

QuantumLeap42 over 10 years

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

boring12345 over 10 years

Would you mind to share your code?

Mike Borman about 10 years

Well this just makes no fucking sense

Joel.Miles925 over 9 years

Would you just avoid saying the f-word?

Ella Hartman over 9 years

???????????

Christopher Chapman about 9 years

this explanation makes me want to swear.

Henry VanDerwater almost 9 years

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

uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh, what?

Ben Winter almost 9 years

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

Isaak Fowler almost 9 years

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.

Answer 50d912113359ab0223000b21

80 votes

Permalink

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);

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

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 11 years

17 comments

Heather S about 11 years

very helpful

greeeen89 about 11 years

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

vbcodervb almost 11 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

Hareesh Hareendran almost 11 years

thanx …it was very helpful

harbingerofdeath almost 11 years

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

Stepan K. over 10 years

Thank you for providing a nice example and explanation

Andreas O. Bø over 10 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 over 10 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 about 10 years

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

tshsugaraa over 9 years

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

SarthakKanyal over 9 years

good answer but i still dont understand

Christopher Chapman about 9 years

well then it is a bad answer..

EthanDebnath about 9 years

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

Rideedee about 9 years

i didn’t get it either

Henry VanDerwater almost 9 years

This one actually makes sense.

Luis Rangel almost 9 years

I hope Ethan didn’t kill himself :(

Lene Akira Kiiro over 8 years

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

Answer 51ef07ac631fe9a98e000290

72 votes

Permalink

// 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
over 10 years

35 comments

Tim Lentz over 10 years

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

Mallorie over 10 years

THANK YOU!!!

Stanley over 10 years

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

Vik over 10 years

Thx-I was befuddled:-(

Koni Segg over 10 years

Thanks

S1monm over 10 years

Cheers! I spent 30 mins looking for a clue.

sainath kollipara over 10 years

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

Ed Chambers over 10 years

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

Ed Chambers over 10 years

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

Erin Ozolins over 10 years

Why 69?

Alex Charters over 10 years

thank you so much

Anthony Myers over 10 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 10 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 10 years

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

JGQ15 about 10 years

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

Awesome, thanks a lot!

kale770 about 10 years

Thanks this helped alot!

Jacque Donahue about 10 years

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

A_fawkes17 about 10 years

Thank you! I was too!

saeberk almost 10 years

So helpful!

Brittany Carver almost 10 years

Thanks!!

Enio Hysa almost 10 years

Thx

Fawaz Zaman almost 10 years

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

gorditobandito over 9 years

this really helped thank you

Cristina Stafie over 9 years

this helped, thanks!

mordye137 over 9 years

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

Jen Yang over 9 years

Thank you! :)

SkittlesIV about 9 years

Hey thanks

Karyan Ng about 9 years

Thank you!

Yasmin Maher about 9 years

thank you

Comfy CouchMedia almost 9 years

nice work @Wes

VikingFanJP almost 9 years

Thanks Wes!

Walker Ray over 8 years

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.

Rabeeh Jouni over 8 years

That’s all I needed !!

Answer 52dffbf9631fe978b80004e6

8 votes

Permalink

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
about 10 years

3 comments

Derek Jones almost 10 years

Sweet, thanks!

sindu666 about 9 years

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

Tomcatgunner over 8 years

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?

Answer 53a343a37c82ca93e300075d

7 votes

Permalink

  • 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
almost 10 years

2 comments

bunghoney over 9 years

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

Jena Attia almost 9 years

THANKSSS’

Answer 520e895180ff339241000f22

2 votes

Permalink

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() {
    var name = prompt("Please enter your name.");
    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() {
    return prompt("Please enter your name.");
};

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
over 10 years

9 comments

boring12345 over 10 years

Made some updates.

ComputerXplorer over 10 years

Thank you so much!!

boring12345 over 10 years

You are welcome! :-)

ComputerXplorer over 10 years

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

987tz2 over 10 years

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

boring12345 over 10 years

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

DinaRowe over 10 years

nooo, you’re not the only one…

Sarah about 10 years

You two arent the only ones too T^T

Henry VanDerwater almost 9 years

WTF?

Answer 5388fc6a548c35d471000e57

1 vote

Permalink

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);

Answer would be 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
almost 10 years

1 comments

sindu666 about 9 years

thank you

Answer 541ada52631fe9507200253c

1 vote

Permalink

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
over 9 years

Answer 544c52187c82caac9b0001e1

1 vote

Permalink

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
over 9 years

2 comments

PIERS CARLTON over 8 years

lel

PIERS CARLTON over 8 years

have fun with that

Answer 552ba61d76b8fe9b8e00033d

1 vote

Permalink

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é
almost 9 years

Answer 5501086d51b8875634001cc0

0 votes

Permalink

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
about 9 years

Answer 53f360cd52f8636ad8000074

-1 votes

Permalink

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
over 9 years

1 comments

alexandru.taran over 9 years

return number/4

Answer 52cf461f7c82cade5000051e

-2 votes

Permalink

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
about 10 years

1 comments

boring12345 about 10 years

Is there a reason why you copied my answer?

Answer 52bf69eb80ff33697300358f

-3 votes

Permalink

// 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 10 years

2 comments

Michael Okropiridze about 10 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 over 9 years

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

Answer 5215b926548c350747001c3d

-23 votes

Permalink

why are we using so many variables

points
Submitted by Gaurav Shahdeo
over 10 years

6 comments

RexGalilae over 10 years

this IS java

Nicolas Schroeder over 10 years

exactly

Ryan Berzinski about 10 years

duh

Eminem5465 over 9 years

Skygate about 9 years

LOL

skystrike959 almost 9 years

java is like that var is very important duh