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 trialchuck debrum
1,792 PointsPassing an argument to a function
WOW...I am just not getting this. I was cruising along through this course and really thought I was so great. Then I got totally stumped on this. I just can't get what he is saying. I am stuck on "Passing an argument to a function Challenge task 2" I just can't seem to interpret what they are asking for. Been stuck on this one task for weeks. Incredibly frustrating. HELP!!!
function returnValue(animal) {
return animal;
}
<!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>
2 Answers
andren
28,558 PointsThe word argument
refers to values that are provided when you call a function. So "passing an argument" basically just translates to calling a function with a value provided.
In JavaScript functions are called by typing their name followed by parentheses like this returnValue()
. Passing in arguments is done by typing the value within those parentheses. Like this returnValue("Some value")
, you can pass in multiple values by separating them with a comma.
To solve task 2 you just need to call the returnValue
function with some value, it doesn't matter if it is a string or a number, so long as you pass some argument.
chuck debrum
1,792 PointsHmmm...i tried both those before asking for help and the challenge told me I was wrong. I guess I had them in the wrong order. I put the variable inside the function and then moved it outside the function. It didn't seem to like anything I was trying. This time though it worked with the variable outside the function. Thank you soooooo much for your help. I was really starting to doubt my ability to pick this up. I'm still not super confident but at least I can move past this challenge.
chuck debrum
1,792 Pointschuck debrum
1,792 PointsYup...It seems I am mostly stuck on part 2 of this challenge. How do I set the value of echo to be the results from calling the returnValue function?
andren
28,558 Pointsandren
28,558 PointsAh, sorry I though it was specifically the passing of an argument part that confused you, not the assignment of the result.
Doing that is actually pretty straightforward. When you call a function that returns a value you can treat the call to that function in the same way you would treat the actual value it returns. In other words you can just assign it by using the = sign normally like this:
var echo = returnValue("Some value")
Since
returnValue
returns the argument passed to it (which is"Some value"
) the above code will end up doing exactly the same thing as if you had written this code:var echo = "Some value"
Since JavaScript will run the function and then just assign the value it returns.