Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Passing an Argument to a Function

Why is this wrong?

unction returnValue (price) { var echo = return price; }

return('cheap');

script.js
function returnValue (price) {
  var echo = return price;
}

return('cheap');
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

There's no need, as far as I'm aware, to set the return value of a function to its own variable, you can just return price straight away.

Also, to call the function you need to use its name e,g, returnValue("cheap")

Brandyn Lordi
Brandyn Lordi
17,776 Points

your script is not written correctly. Check out my example below.

function returnValue(price) {
  return price;
}
returnValue('value');

Notice return price is nested within my function in my example. After creating the function, you call the function, and pass the argument (or parameter) - in this case, i pass value. It returns my given parameter because i tell it to do so in my function with return price

You could alter your current function to work by making the following adjustments:

function returnValue (price) {
  var echo = price;
  return echo;
}
returnValue('cheap');

Does this make sense?

Zack Jackson
Zack Jackson
30,220 Points

Not sure why Brandyn was down voted. His response was correct.

This part of your code sets up a function called returnValue where you are passing into the function an argument named price.

function returnValue (price) {
  ...
}

This part of your code is just incorrect syntax. Not sure what you're trying to do here, but you passed in price and you want to return it and assign it to a variable called echo? Why? Maybe you can explain what you're trying to do with this code so that we can better help you get it working.

  var echo = return price;

This part of your code is also just incorrect syntax. Your function is called "returnValue", not "return". The cheap that you're passing in is the price arg from above. Your function should do something with this. See example below.

return('cheap');

For instance, if you had the function return a sentence or whatever that included the price that was passed in. This passed in arg is being concatenated onto this string. The result would be "This item is cheap." being returned.

function returnValue(price) {
  return('This item is ' + price + '.');
}

returnValue('cheap');