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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Adding Data to Arrays

have I got this print function parameter passing thing right?

/*.........print function.......*/
function print(message) { /* to be printed passed to print(message) */
  return document.getElementById("print").innerHTML = message;
  /* innerHTML = message is passed to (message)
  and then printed to the page */
}

2 Answers

You use return when you want something back from your function, e.g., a value, . Take this example:

function square(x) {
   return x * x;
}

You give the function a value, x, and you want it to return the square of that value.

But in your case, you didn't need any value back. You just wanted the function to print a message to the page.

Thanks j, I think I get it.

If you just want to display message on the page, then there's no need for the return. Just do it like this:

function print(message) { 
  document.getElementById("print").innerHTML = message;
}

This code assumes there is an element on the page with an id of print.

there is an id of print on the page for my innerHTML, and I thought Dave was using a return but I looked again and he wasn't. I'm still foggy on when return is necessary. Thanks for your feedback.