c# – What is the difference between an argument and a parameter?

Question:

What is the difference between an argument and a parameter in the C # programming language?

Answer:

A parameter (formal parameter) of a function is a variable in a function that will contain the input value passed from outside.

An argument is an actual variable or expression whose value is used as an input value when the function is called.

Example:

double Negate(double x)
{
    return -x;
}

Here x is a parameter of the Negate function.

double r = Negate(5.0);

Here 5.0 is the argument of the call to the Negate function.

Thus, the set of parameters (that is, variables "passed from outside") of the function is fixed, but the arguments (that is, expressions whose values ​​will fall into these variables) can be different for each of the calls.

Proofs.


However, these two terms are very often used as synonyms. If you accidentally use one instead of the other (not on an exam!), You will be easily understood.


As @StateItPrimitive suggests, in C #, when calling a function, you can specify arguments in an order that does not necessarily match the order of the parameters by using named arguments : precede the argument with the parameter name. This can be useful for long parameter lists, so you don't have to keep in mind their order:

string ComposeName(string firstName, string lastName)
{
    return $"{lastName}, {firstName}";
}

string fullName = ComposeName(lastName: "Hejlsberg", firstName: "Anders");

Or just for better readability of the text, if the meaning of the parameter is not obvious, based only on its type:

await client.DownloadFile().ConfigureAwait(continueOnCapturedContext: false);
Scroll to Top