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

Function in JS or any programming languages

How does the function works? Can someone explain when we'll use it and how we can use it or something like those, it would be really helpful for me.

3 Answers

Yes, it stops the function, and returns, or gives, the value to the caller of the function. If you don't return anything, the function will come back undefined. So the short answer is no. You don't NEED a return statement for functions, because it will just return 'undefined'. However, if you wish to do something with the values that you created within the function, then you will need a return statement. As far as Javascript engines are concerned, EVERY function returns something. Whether it be 'undefined' or whatever you want to return using the return keyword.

Hope this helped!

A function in most cases are blocks of code that can be reused multiple times without the need of having to rewrite the same block of code over and over.

lets say you had an application that took 2 numbers and added them together. you could create a function to write a reusable block of code.

That function could look something like this.

function addTotals(num1, num2){
    return num1 + num2
}
/* 
*  num1 and num2 can be named whatever
*  you'd like but its best to label them
*  something relative to what they are
*/

console.log( addTotals(2,5) ); // 7
console.log( addTotals(5,10) ); // 15
console.log( addTotals(10,20) ); // 30

I hope this helps.

With your answer I clearly understand the power and how useful of the function. Really appreciate, but what does the return do in function?

A return statement returns a value to the caller of the function. So, in the code above, when he says 'return num1 + num2' it stops the function, and returns the values that are passed into the function, to the caller of the function.

Whatever the numbers are being passed into the function, they will always be added, unless the return statement is changed.

Here is a MDN article on return statement that you should really take the time to read: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

I would really recommend visiting the MDN (Mozilla Developer Network) for questions as well! It's a great resource for any questions that you might have!

Hope this helped!

function square(y) {
 var total = y + y;
 return total;
}

alert(square(4));

What I know is the return is used to return something to function and/or stops the function <<< Right or wrong?

If we don't specific total after the return, my function will do nothing and useless <<< Right or wrong?

Does return is a must for every function? If not, I wonder what type of situation when we don't need to use return.

Thanks very much!