What is the difference between parameter and argument?

Asked

Viewed 26,094 times

151

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 another term, in English and Portuguese, sometimes being used one or the other, but I could not perceive any criterion in the choice between one and the other.

Is there a difference between them? Or are they really synonymous? I have come to think that "parameter" was the only correct term (because 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?

  • 12

    Big question, always have the same doubt. No more :)

  • 3

    The parameter is the requirement that the enterprise function for the value to be passed as argument (which may be valid or not) :)

7 answers

128


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

You don’t pass parameters, you passes arguments. You get arguments too, but you get in parameters. You parameterizing your function with information that will come later. You argues with what you want to execute a properly parameterized function.

There may be fewer arguments for each parameter since there are parameters that are optional and others that may 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 myself admit that I exchange the terms erroneously, but for good communication it is important for everyone to know the correct.

I found a OS question 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 in the Github for future reference.

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

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

Here is an important detail on how to pick up arguments in mgibsonbr’s first comment below. There are other languages that have similar resource. It is possible to access the arguments without knowing exactly what the parameters are.

  • 7

    It makes sense: Javascript for example exposes the list of values used in the call through the variable arguments - which may be larger or smaller than the number of parameters declared. And in languages that accept optional parameters and/or parameters default, no matter how the function was called, its body always has access to a fixed list of variables (albeit with null values). I never thought of it that way...

  • 4

    @mgibsonbr is, you have access in this way to the list of arguments and not declared parameters. Theoretically you could have access to the parameters as well, but in general the utility would be limited. I don’t know in JS (probably gives by the philosophy of the language), but in Java and C# you can get the list of parameters by Reflection (this you know :) )

  • 4

    This is unnecessary in Javascript because there is no function overload, no static types, and any function can be called with any number of, Aham, arguments (I’m already getting used to... :P). The most you can get is the number of formal parameters, through the property length (fountain).

  • 3

    In Javascript the only way to get the list of parameters is to get the source code of the function with toString() and parse them from the statement itself (does not work with functions built-in). But I never needed it... @mgibsonbr

  • 1

    I don’t know if it’s bullshit what I’m talking about, but then why not python declare def fn(**kwargs) instead of def fn(**kwparams)????

  • 1

    Because the variable kwargs is a parameter, but its content is a list of arguments. The name of the variables should say what is in them, not what they are in the code.

  • I do not have as much authority on the subject because I have not seen so many debates on the terms (so if you have reference and how to refute, I am open-handed), but in my academic studies I have learned that the values passed in the function call (which here is being called "arguments") are known as the real parameters and those defined in the function/procedure header are the formal parameters.

  • I know that people abstract the idea of real parameters as "arguments", this is valid, just as people abstract the idea of formal parameters only by saying that they are parameters... I see no problem in this, but I disagree with the fact that "You do not pass parameters"... because arguments are real parameters.

  • 1

    @Gabrielkatakura may even be, may be used in academic scope, perhaps in a specific scope, or in another scope. No place I’ve researched talks about it. Which obviously just means that. In no way am I saying that it is wrong, on the contrary, it makes a lot of sense, even if I do not like this definition. Maybe you can give an answer. If you have any references that talk about it would help a little, but nothing that is required. I believe you. I find his last comment contradictory, says he accepts, but does not accept... :) If accept is just that.

Show 4 more comments

55

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

inserir a descrição da imagem aqui

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 take the arguments;
    • The name of each parameter serves as a local variable within the function;
    • A good mnemonic is to think that a PIameter 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 Torgumento is the value Totual.
  • 1

    These analogies are very good to explain the subject.

  • Show! With the illustration got even better.

45

A parameter is the variable which 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.

21

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. Call code provides the arguments when calling the procedure.

14

Most programmers use these terms without distinction of meaning. In practice, saying the word wrong or right 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 better understand see this example in Javascript:

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

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

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

On the other hand we have the argument, which would be the value passed (may 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 for 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 "valueSoma ". It is important to note that variables are neither parameters nor arguments.

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

I have heard definitions (and they are still true) that an argument could be the parameter instance, not literally, but it is an interesting analogy.

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

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

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

References:

1

In short:

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 ARGUMENTS (1 + 2) OR (5 + 50) ETC.

-2

For example:

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

multiplicar(4, 3) # Argumentos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.