What is the difference between parameter and argument?

116

I have always used the terms "parameter" and "argument" as if they were synonyms: what is passed to a function and / or what the function receives as input. In the same way, I have been reading one and another term, in English and Portuguese, sometimes being used one way or another, but I could not perceive any criteria in the choice between one and the other.

Is there a difference between them? Or are they even synonyms? I already came to think that "parameter" was the only correct term (since it is used in mathematics) and that "argument" was some translation error, but then I realized that this is not the case (in English we also have "and" argument "). What is the correct way to use these terms?

    
asked by mgibsonbr 13.09.2014 в 23:40
source

5 answers

108

Parameter is the variable which will get a value in a function (or method) while an argument is the value (which can be derived from a variable or expression) that you pass to the function (or method).

You do not pass parameters, you pass arguments . You get arguments too, but you get Parameters . You parameterize your role with information that will come later. You argue with what you want to perform a function properly parameterized.

There may be fewer of the more arguments for each parameter since there are parameters that are optional and others that can be variable lists of data. So there is no one-to-one relationship and the distinction between them is important.

It is often confused by everyone and I admit that I exchange the terms wrongly, but for good communication it is important for everyone to know the right one.

I found a question in the OS with some answers on the subject.

Example

void Func1(int i, bool flag = true) { //declarou dois parâmetros
    // execução
}

void Func2(params int[] lista) { //declarou um parâmetro
    //execução
}

void Func3(bool x, bool y) {
    int z = 0;
    Func1(1); //chamou com 1 argumento
    Func1(z, x && y); //chamou com dois argumentos vindos de variável e expressão respectivamente
    Func2(1, 2, 3); //chamou com 3 argumentos
    Func1(flag : false, i : 2); //argumentos nomeados
}

I've placed it on GitHub for future reference .

Remembering that all parameters are named. At least that's the way it is in most languages. The variable name is the name of the parameter.

In dynamic languages the lack of direct relation between parameters and arguments becomes even more evident.

See an important detail on how to pick up arguments in the first comment below. There are other languages that have similar functionality. You can access the arguments without knowing exactly what the parameters are.

    
answered by 13.09.2014 / 23:53
source
35

A parameter is the variable that you declare in the function signature. An argument is the value that you pass to the variable when you call the function. But this distinction is purely conventional.

    
answered by 13.09.2014 в 23:52
30

The function defines the parameter, and the call code passes the argument to this parameter.

By analogy, we can consider the parameter as a parking space and the argument as a car.

So ...

  • Parameters
    • They are defined in the declaration (or signature) of the function / method;
    • Parameters of a function are given the arguments;
    • The name of each parameter serves as a local variable within the function;
    • A good mnemonic is to think that a P arameter is like a P laceholder for a P
  • Arguments
    • Represent the current values / variables passed to the function parameters when it is invoked;
    • Each argument corresponds to a parameter (in the same position);
    • A good mnemonic is to think that a A argument is A value.
answered by 08.01.2017 в 17:13
14

Parameters

Parameter represents a value that the procedure expects you to pass when you call it. The procedure declaration defines its parameters.

Arguments

An argument represents the value that you pass to a procedure parameter, when you call the procedure. The calling code provides the arguments when calling the procedure.

    
answered by 09.08.2016 в 17:23
7

Most programmers use these terms without distinction of meaning. In practice, saying the wrong term or certain will not make the code work or not, it will depend on you, but it is always good to know and understand the differences.

Imagine a parameter such as a variable that can be received by a function or a method . For a better understanding, see this example in JavaScript:

function somar(param1, param2) {
   return param1 + param2;
};

This function will always receive two parameters (param1 and param2). In addition your exit will always depend on the entrance. So with this little explanation we can already conclude that the parameter will always be bound to its input.

The rules for how parameters are passed to functions are determined by the programming language, for example, these rules can specify the order of parameters, whether they are passed from left to right, or vice versa. In addition rules can define whether parameters will be passed by reference or value .

Note: Parameters can be of any type, an array, an integer, a String, or other data type (of course this varies in programming languages).

On the other hand we have the argument, which would be the value passed (may be more than one) function at the time of your call. See:

let valor1 = 10;
let valor2 = 20;

let valorSoma = somar(valor1, valor2);

During the above code execution, the variables "value1" and "value2" are initialized and passed as arguments for the somar() function. Within it, the parameters are evaluated and the arguments '10' and '20' are obtained. These values are summed, the result is returned and assigned to the variable "valueSum". It is important to note that variables are not parameters or arguments.

I believe that only with the above explanation can you understand the logic and difference parameters and arguments, but we will see in simpler ways what each one can mean.

I've heard definitions (and they're true) that an argument could be the parameter instance, not literally, but it's an interesting analogy.

Another way to identify (not to forget) the difference of each:

  • Parameter - > Something that will receive the values when calling the function.

  • Argument - > Value / object or anything you put inside the function call.

References:

answered by 02.10.2018 в 14:32