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 Passing an Argument to a Function

Adam Wieckert
Adam Wieckert
6,834 Points

Returning value from function to variable

I am on step 2 of the challenge question which states

After your newly created returnValue function, create a new variable named echo. Set the value of echo to be the results from calling the returnValue function. When you call the returnValue function, make sure to pass in any string you'd like for the parameter.

I am unsure as to what is incorrect about the code I have written.

function returnValue (adam) { return adam; }

var echo = returnValue(); returnValue("String");

script.js
function returnValue (adam) {
  return adam;
}

var echo = returnValue();
returnValue("String");
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>
Luis F. Estevez
Luis F. Estevez
4,219 Points

You have to pass the parameter when you call the funcion in the same line:

var echo = returnValue ("Some text");

2 Answers

Steven Parker
Steven Parker
231,269 Points

Your code creates "echo" and sets it to the return value of the function. And then on a separate line you call the function and pass it a string.

Now just combine those together into one statement that creates "echo" and sets it to the return value of calling the function with the string.

Adam Wieckert
Adam Wieckert
6,834 Points

Thanks for the help. The confusing thing here is that this code:

var echo = returnValue("String");

it does not appear, to me, to be "Calling" the function. This code is setting the variable equal to the function with an argument of "String".

One would then need to "Call" the function with the argument of "string" again to make it run, which in my head seems redundant.

When you set a variable equal to a function does it inherently call the function?

I suppose we may have already seen this behavior when we set Var = prompt("String"); and saw a prompt appear....

Thanks again man!

Steven Parker
Steven Parker
231,269 Points

You "call" a function when you put parentheses after the name, with or without argument(s).

To set a variable to the function itself (instead of the result of calling it), you use only the name.

Luis F. Estevez
Luis F. Estevez
4,219 Points

Yes, it calls the function and takes the value the function is returning. But you don't need to declare your var and set it to a function in the same line. You can do this:

var echo;
echo = returnValue ("Some text");
Steven Parker
Steven Parker
231,269 Points

The important thing is to pass the argument and set the variable in the same statement.