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
Joel Pendleton
19,230 PointsWhy do we use arguments in functions?
What is the purpose of using parameters / arguments in functions? What are the benefits of them? An answer with an example would be appreciated.
3 Answers
Chyno Deluxe
16,936 Pointsadding parameters are beneficial because it will allow you create reusable code without the need to repeat yourself.
example
function getDrink(type){
console.log(type);
}
getDrink("coffee"); // coffee
getDrink("sweet tea"); // sweet tea
getDrink("milk"); // milk
Here is another example using two parameters
function buyDrink(type, cost){
var wallet = 5;
console.log(type, "$" + (wallet -= cost) + " left to spend");
}
buyDrink("coffee", 3); // coffee $2 left to spend
buyDrink("sweet tea", 1); // sweet tea $4 left to spend
buyDrink("milk", 2) // milk $3 left to spend
I hope this helps. Feel free to ask me any questions you might still have.
jason chan
31,009 PointsSo we can pass in a function. Think of arguments like putting numbers to solve in excel functions. Can you believe somebody wrote all those functions for you to use in EXCEL? LOLs.
Chyno Deluxe
16,936 PointsHaha. yeah even with javascript knowledge I don't think I can create an excel function haha.
But a last bit of info on the clarification of parameters and arguments.
function funcName(parameter1, parameter2){
//do something
};
functions use parameters as the placeholders for the values you will put in but when you call a function the values you put in are then called arguments
funcName(argument1, argument2);
It was something that always confused me so I thought i'd share the information to help you if you didn't already know.
Joel Pendleton
19,230 PointsJoel Pendleton
19,230 PointsHow does it allow you to create reusable code?
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 PointsAs you can see in the examples above. I only wrote the function once but then I call the function with different arguments(i.e coffee, sweet tea, milk).
Those arguments get pushed through the function and any script within the function then gets called using the arguments just as you see in the second example.
Each item has a different cost and then returns how much money is left to spend.
Joel Pendleton
19,230 PointsJoel Pendleton
19,230 PointsSo would you say that an argument allows you to more easily input information into a function for it to be manipulated as opposed to taking data from a variable defined inside the block of the function or defined globally.
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 PointsCorrect! The manipulation all varies on the function itself but it can be as simple as console logging a value, to taking the value of an argument and creating a complex chain of events based on the argument(s).
The examples I provided have the arguments being pushed within the javascript file itself for demonstration but a real world scenario would likely receive those values from an input after a submit event.
Joel Pendleton
19,230 PointsJoel Pendleton
19,230 PointsIf I was to remove the parameters in your example would the undefined keyword have no value? If that's so, the only way to give them value would be to define a local or global variable for them but this method (of the defining the variables explicitly) makes the code the un-reusable because you would have to adapt the variable names and values for new data - is this right? Also, how would you say the the parameters are related to their mentioning in the function block? Would you say they're dependent on the parameter existing and the argument's values, otherwise they wouldn't work in the function or hold any value?
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 PointsThis is correct. undefined, in this case, means you are passing a value while the function has no parameters. In other words, you are giving the function information that means nothing to the function and therefore will not be able to use that value.
The parameters themselves ARE the variables. When you create parameters in a function those names then can be used normally like variables. Depending on the function and the script within it, the function would be useless without the parameters, IF it's expecting to receive a value.
Joel Pendleton
19,230 PointsJoel Pendleton
19,230 PointsThank you for your help Chyno. :)