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 trialja5on
10,338 Pointswhy do we use parameters
why do we use parameters, before Guil used them the program worked in the same way so why do we use more time to use them when they dont change the expected output.
1 Answer
J A
Full Stack JavaScript Techdegree Student 4,646 PointsGood question. It's about making your code more re-usable in the future. In Guil's solution, the randomRGB()
function accepts a value
parameter. This value
parameter could be any function. In our case, we've defined it as a function that returns a random value. But imagine in another place in your code you had a different algorithm for determining the RGB value.
For example, you could pass a function that returns an RGB value based on the current temperature at your location or based on the user's mouse location, instead of a random RGB value. In that case, you can wrap that algorithm in another function and then pass that to randomRGB()
as the value
parameter. The randomRGB()
function is completely unaware of what the function stored in value
is doing, only knowing that it returns a value it can use. This is called abstraction. The randomRGB()
is now only concerned with constructing a string like rgb(11,22,33)
, it doesn't care where those values come from or how they were calculated.
Jerry Kankelborg
4,885 PointsJerry Kankelborg
4,885 Pointsbecause as Guil mentioned, it is more efficient. even though the final output is the same, the computer used less memory to compile because this time, only one function call was executed, instead of the three that were in the initial program. As you progress in your learning you will use parameters daily and this will smooth itself out.