What is the difference between parameter and argument?

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 read one and the other term, in English and Portuguese, sometimes being used one or the other, 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 have come 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 is not the case (in English we also have "parameter" and "argument"). What is the correct way to use these terms?

Author: Maniero, 2014-09-13

7 answers

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

You don't pass parameters, you pass arguments. You get arguments as well, but you get in parameters. You parameterize your function with information that will come later. You argue with what you want to execute a function properly parameterized.

There may be fewer 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.

Is often confused by everyone and I myself admit that I Exchange the terms erroneously, but for good communication it is important for everyone to know the correct 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 put on GitHub for future reference.

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

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

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

 126
Author: Maniero, 2020-09-08 20:12:16

The function sets the parameter, and the call code passes the argument to that parameter.

insert the description of the image here

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

Therefore ...

  • parameters
    • are defined in the declaration (or signature) of the function/method;
    • the parameters of a function receive the arguments;
    • the name of each parameter serves as a local variable within the function;
    • a good mnemonic is to think that a Parameter is like a Placeholder for a potential value.
  • arguments  
    • represent the current values/variables passed to the function parameters, when invoked;
    • each argument corresponds to a parameter (in the same position);
    • a good mnemonic is to think that a torgument is the value totual.
 50
Author: SandroMarques, 2019-10-12 15:14:57

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

 44
Author: nbro, 2018-10-11 09:36:17

Parameters

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

Arguments

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

 20
Author: Marco Souza, 2018-03-20 19:24:39

Most programmers use these terms without distinction of meaning. In practice, speaking the wrong or right term 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 like a variable that can be received by a function or a method. To understand better see this example in JavaScript:

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

This function will always receive two parameters (param1 and param2). In addition to its output will always necessarily depend on the input. Therefore, with this small explanation we can already conclude that the parameter will always be linked 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 will be passed from left to right, or the other way around. Also rules can define whether the parameters will be passed by reference or value .

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

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

let valor1 = 10;
let valor2 = 20;

let valorSoma = somar(valor1, valor2);

During the execution of the above code, the variables "valor1" and "valor2" are initialized and passed as arguments to the function somar(). 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 "value ". it is important to note that variables are neither parameters nor arguments.

I believe that only with the explanation above it is possible to understand the logic and difference parameters and arguments, but let's see in simpler ways what each can mean.

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

Another way to identify (not to forget anymore) 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.

References:

 13
Author: João Pedro Schmitz, 2018-10-04 14:37:09

In summary:

Parameter can be understood as a variable (location), a place, a container, etc.

On the other hand, the argument is the "current" value of the parameter.

Example:

(X + Y) WHERE "X" AND "Y" ARE PARAMETERS THAT WILL RECEIVE THE ARGUMENTS (1 + 2) OR (5 + 50) ETC.

 1
Author: Dahlin Dos Santos Gomes, 2020-12-09 13:05:14

For example:

def multiplicar(x, y): # Parâmetros
    return x * y

multiplicar(4, 3) # Argumentos
 -2
Author: Felipe Soares, 2020-11-13 12:10:29