As a programmer, we have probably encountered the terms argument and parameter many times. They are often used interchangeably, but they both have different meanings. In this post, we will explain the difference between an argument and a parameter.

1. What is an Argument?

An argument is a value that we pass to a function or a method when we call it. An argument can be a constant, a variable, an expression, or another function. Arguments represent the actual values supplied to a function. For example, in the following code, we call the function pow() with two arguments: 2 and 3.

 
It is worth noting that variables can be arguments. If the function is called as below, then the variables x and y are arguments to the pow() function, not the values 2 and 3.

 
Arguments can be passed to a function in different ways, depending on the programming language and the function definition. Some of the common ways are:

  1. By value: The argument value is copied to the corresponding parameter in the function. Any changes made to the parameter do not affect the argument.
  2. By reference: The argument is passed as a reference to the corresponding parameter in the function. Any changes made to the parameter affect the argument as well.
  3. By name: The argument is passed as a name or an expression to the corresponding parameter in the function. The argument is evaluated only when it is used in the function.

2. What is a Parameter?

A parameter is a variable that is declared in the function or method definition. A parameter can have a name, a type, and a default value. Parameters are also known as formal parameters because they represent the formal names that are used in the function definition. For example, consider the following function definition: Here, the add() function takes two parameters, x and y. If the function is called as add(2, 3), then 2 and 3 are the arguments.

 
The easiest way to distinguish between an argument and a parameter is to look at where they are used in the code. Arguments are used in the function call, while parameters are used in the function definition. Arguments are the values that are passed to the function, while parameters are the variables that receive the values.

3. Key Points

  1. A parameter is also called a formal parameter or formal argument, and an argument is often called actual arguments or actual parameters.
  2. A parameter is optional in some programming languages, and some programming languages allow for a default argument to be provided in a function’s declaration. This means that while calling the function, the caller can omit that argument.
  3. A parameter has a name, a data type, and a calling mechanism (call by reference or call by value). In contrast, an argument is an expression that does not have any name, but it can be a variable, a constant, or a literal.
  4. The scope of a parameter is the function itself, and it serves as a local variable inside the function.
  5. The number of arguments in a function call should match the total number of parameters in the function definition. One exception to this rule is functions with variable-length parameter lists.
  6. The data type of arguments in a function call should match with the data type of parameters in the function definition.
  7. If a parameter is passed by value, modifying it inside the function does not change the corresponding argument in the caller. However, if it is passed by reference, the values of the corresponding argument can be changed in the caller.

That’s all about the difference between an argument and a parameter.

 
References: Parameter – Wikipedia