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

Jordan Lungwe-Forde
Jordan Lungwe-Forde
737 Points

Can someone explain why adding multiple parameters to functions can be useful

I'm currently learning how to add parameters to functions and I think I get it. I'm just struggling to understand why this can be beneficial.

In addition to this, how the relationship between parameters and arguments be described?

Many thanks in advance!

1 Answer

Alessandro Calorì
Alessandro Calorì
10,241 Points

Functions are a mathematical concept: you take some values in and return a value as result. In math functions are written as follows "y = f(x1, x2, ... , xn)" where "y" is the result of the computation and "x1, x2... xn" are the parameters used by the function "f" to calculate the outcome.

Functions in computer programming are the same: you give them a variable number of parameters and you get a value back. I make an example to make things clearer: suppose you want to write a function "Add" which takes in two numbers and returns the sum of the two values. A function such this is useless if it would take only one parameter.

function Add(a, b) {
  return a + b;
}

Most of the functions you will be working with will have multiple parameters. More advanced programming languages such as Go and C# (with the recent addition of tuples in the language syntax) allow also to return multiple values from a function (for example the result and a status message).