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 Giving Information to Functions

Omar Elfishawi
Omar Elfishawi
4,060 Points

Confused - Giving informations to function' s logic

function getRandomNumber( upper ) { var randomNumber = Math.floor( Math.random() * upper ) + 1; return randomNumber; }

function getArea (width, length) { var area = width * length); return area; } }

This is a transcript from the lesson. I dont understand how Math.random can multiply to upper, when upper is nothing, not a method, not a stored value, just a series of character. Im new to this, please help me understand the logic, tks

/* console.log(getRandomNumber (6) ); console.log(getRandomNumber (100) ); console.log(getRandomNumber (1000) ); */

4 Answers

When you call a function, you can pass an argument, the function will take it and (probably) do something with.

exampleFunction(argument);

For example, passing the number 5 as an argument

exampleFunction(5);

When you define a function, you assign that argument to a parameter. In this example, the function would store the number 5 I just passed (argument), to the parameter "input", it will act as any other variable. Then you can do what you want with it.

function exampleFunction(input) {
alert(input);
}

I think the challenge should go like this

function returnValue(Giovanni) {
return Giovanni;
}

var echo = returnValue(100);

Here, you create a new variable named echo, and its value will be whatever the returnValue function returns. You call the returnValue function and pass it an argument, the number 100. The function isn't really useful, because it just takes the argument you give and returns it again. It doesn't do anything. Giovanni is the parameter of the function, whatever data you pass to it, it will get stored temporarily in the Giovanni variable, it only exists within the function. In this case, Giovanni = 100. If later you call the function again and pass a string as an argument, then Giovanni = string.

Thank You axelperossa. I got it now.

Charles Wanjohi
Charles Wanjohi
9,235 Points

upper is the value you pass to the getRandomNumber() function e.g. for getRandomNumber (100) the upper in this case is 100

Omar Elfishawi
Omar Elfishawi
4,060 Points

thanks Charles, really appreciate your help. I am sorry i am still confused.

maybe is the name that confuses me. upper is not a method, am I right? I mean, it could be called "x" "argument1, argument2" or "Giovanni" anything right? It simply stores the value that the argument passes it ...?

Charles Wanjohi
Charles Wanjohi
9,235 Points

Yeah.....thats correct

Omar Elfishawi
Omar Elfishawi
4,060 Points

Thanks Charles. Im afraid i am still very confused about parameters. im stuck on the next quiz:

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.

The function will accept the string you pass to it and return it back (using the return statement). Then your echo variable will be set with what is returned, the results.

My attempt:

function returnValue (Giovanni) { var echo = returnValue ; return (Giovanni) }

returnValue(Giovanni);

I laugh at myself. From what i understand an argument is similar to a value you assign, like a 9 or 0.9 in

int some_whole = 9 float some_decimal = 0.9

The parameter equals data type in C?

I undertand the logic of "get coffee and croissant from coffeeshop" explained in the video, but i am confuesed on how it translates in syntax