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 Create a max() Function

Really unsure what I'm doing here. Having some troubles with functions. I understand that I can set parameters...

but I don't understand this:

do I then create variables to ask for the numbers? or I am strictly assigning the values to the numbers when I call the function (because I've set parameters)?

I suppose I'm just not understanding the lesson in general. Any help would be most grateful to truly understand functions and the assignment. Rewatched videos twice in this block.

Thanks, Erik

script.js
function max (x, y) {
  if (x > y) {
    return alert("The largest number is " + x + "!");
  } else {
    return alert("The largest number is " + y + "!");
}

1 Answer

or I am strictly assigning the values to the numbers when I call the function (because I've set parameters)?

Exactly. When you create parameters in a function, they become the variables when you call the function.

It is said that function parameters are the names listed in the function definition and function arguments are the real values passed to (and received by) the function. <-- Stole that from w3schools.com :metal:

Think of it like having a function that you can call with different values to obtain different results. It's one of the first steps in getting away from hard coding values.

for example, in the function below, i'm calling it 2 times with different arguments to get different results...

function getName(name, age) {
return "You are " + name + " and you are " + age + " years old"; 
}

console.log(getName("Chris", 40));
console.log(getName("Colleen", 39));

You would write your example like this...

function max(x, y) {
  if (x > y) {
    return "The largest number is " + x + "!";
  } else {
    return "The largest number is " + y + "!";
    }
}

console.log(max(10, 9));

I hope this helps :thumbsup: