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 Parameter question

I am stuck on this question...

Call the function by giving an age in the space for a parameter.

function random_age(age) {

  Math.floor(Math.random() * age);

}

random_age();

Exactly how it is above is the code given to me that is wrong and I need to fix it. The parameter is function random_age(age) the word age between the parentheses, correct? So, as I see it, it appears already correct to me. But I've tried putting age between other parenthesis, all of the parenthesis and not changing it at all and nothing is taking. Can someone explain this to me?

2 Answers

Sun-Li Beatteay
Sun-Li Beatteay
10,606 Points

The "calling" of a function is done after you set the function. So in your above code, the function is set:

function random_age(age) {

  Math.floor(Math.random() * age);

}

and to call it, you do:

random_age("insert age here");

So the question is asking you merely to put in an age number where I wrote the "insert age here" because that is your parameter. If you create a function that requires a parameter, you cannot call it without giving it that parameter. In this case, it's an age. S

So one answer could look like this:

random_age(5);
Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Sun-Li Beatteay gave a very nice answer! However, I'm going to point out some semantic differences. Some programmers use the terms "argument" and "parameter" interchangeably. However, this is not entirely correct in the strictest sense. The parameter is in the declaration and definition of the function. The argument is what goes into the parameter when you call (invoke/execute) the function. Here's an example:

function addTwo (x, y) {
  return x + y;
}

var result = addTwo(5, 20);
console.log(result);

In this example, the function has two parameters x and y. And it accepts two arguments when we call the function. The arguments are 5 and 20. Try and think of it this way: a parameter is what the function accepts as information coming in. The arguments are the information sent to the function when we call it.

Hope this helps!

Sun-Li Beatteay
Sun-Li Beatteay
10,606 Points

Thanks Jennifer for clarifying that! I do interchange them too often. You're very correct that they are different.