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

For the function to create the 3 buttons (up, down, remove). Why was a return statement not used?

Is it because we didn't list any variables, so there were no parameters used, is that when we don't use a return statement in a function?

1 Answer

HI Gremyko!

I hope I'm understanding your question (without seeing the actual course/video/challenge)...

This seems to be an example of a function that does only what can be referred to as "side-effects", meaning the function changes state (somehow manipulates the DOM or data), but doesn't return any value.

You only need to return a value if your function is transformative (such as you input a value and return that value altered in some useful and consistent way, such as putting bread in a toaster and getting back toast) or if you need to return a true/false value to acknowledge whether the function was successful in its mission and that outcome would affect the program logic (such as the condition in an if statement).

So the real question is: what is the purpose of the function?

In this case, you are creating buttons and if you don't need to track (for program flow) whether the buttons get created successfully or not, then you don't need to return anything. But if other parts of your program would behave differently depending on whether the buttons get created or not, then it might be useful to return true if they are created successfully and false if they are not.

Does that make sense?

BTW, I'm led to believe that in functional programming this kind of function is considered not best-practice.

Of course, it is also possible to write a function that creates the buttons and returns them in, say, an array, to be used by other parts of the program - I'd have to see the actual context to know for sure, though.

Keep in mind, the whole point of creating functions at all is to keep your code DRY (Don't Repeat Yourself). Meaning you create a function when you encounter smaller bits of code that you need to use over and over in several parts of your program. Instead of retyping the smaller bits of code over and over, you create a function and then just call the function multiple times (whenever required).

This article might help:

https://medium.com/javascript-scene/master-the-javascript-interview-what-is-functional-programming-7f218c68b3a0

I hope that helps.

Stay safe and happy coding!