Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Giving Information to Functions

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Can someone explain this code to me?

What's the logic behind this function? I still feel like I don't get the role of return. So, we create a function and we pass two parameters to it (the width and the length, which can be named however we like).

Then we have a block code with a variable which is necessary to determine the formula that will help us find the area of the square.

Why do we use return? What happens with the "area" value in return? Where does it go and how does it help in running the getArea function?

                     function getArea (width, length) {

                                    var area = width * length;
                                   return area;
                                }

                   alert(getArea(10, 20));

2 Answers

Blake Wallace
Blake Wallace
3,864 Points

Alright so I just finished this course and I think I understand it so here are my thoughts on it. So in the code you posted you are creating a function that you can send arguments to find the area of (for example) a room. In math we would take the length and multiply it by the width. Let's break this down.

First we name the function and parameters like so.

function getArea (width, length) {

Now in the command block we add the formula.

var area = width * length; 

Now the variable we just named is the finished product. It stores the width multiplied by the length, giving us the area of a room. However we need that variable sent back to us. That is where return comes in.

   return area;
}

So now anytime we call the getArea function we can feed it two arguments and it will "return" the "area" back to us.

alert(getArea(10, 20));

In this instance, the function returns the area in an alert box. You could have it printed to the page using document.write, or to the console using console.log. Either way the return statement is ending the function and returning back the value we are calling for.

Hope that answers your question.

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

So return is only useful if we want to do something with the result of our function. Otherwise the result is just returned to us.

Blake Wallace
Blake Wallace
3,864 Points

Basicly. Return is simply ending the function and returning a value. For examples of applications of creating a function that simply returns a value, refer back to the video getting information from a function starting at 3:01. https://teamtreehouse.com/library/javascript-basics/creating-reusable-code-with-functions/getting-information-from-a-function.