Reputation: 5156
When verbally talking about methods, I'm never sure whether to use the word argument or parameter or something else. Either way the other people know what I mean, but what's correct, and what's the history of the terms?
I'm a C# programmer, but I also wonder whether people use different terms in different languages.
For the record I'm self-taught without a background in Computer Science. (Please don't tell me to read Code Complete because I'm asking this for the benefit of other people who don't already have a copy of Steve McConnell's marvelous book.)
Upvotes: 1103
Views: 475876
Reputation: 8077
Quoting the C++ standard:
https://eel.is/c++draft/intro.defs#defns.argument
3.2 argument
⟨function call expression⟩ expression in the comma-separated list bounded by the parentheses
So in f(25 + 4)
(function call), the 25 + 4
expression is an argument.
https://eel.is/c++draft/intro.defs#defns.parameter
3.37 parameter
⟨function or catch clause⟩ object or reference declared as part of a function declaration or definition or in the catch clause of an exception handler that acquires a value on entry to the function or handler
So in int f(int x);
(function declaration), int f(int x) { return x + 1; }
(function definition), and try { std::vector<int> v; std::cout << v.at(3); } catch (const std::out_of_range& x) { std::count << x.what(); }
(exception handler), the x
expression is a parameter.
Upvotes: 1
Reputation: 74641
Upvotes: 0
Reputation: 19498
Let's say you're an airline. You build an airplane. You install seats in it. Then, you fill the plane up with passengers and send it somewhere. The passengers disembark. Next day, you re-use the same plane, and same seats, but with different passengers this time.
The plane is your function.
The parameters are the seats.
The arguments are the passengers that go in those seats.
function fly(seat1, seat2) {
seat1.sayMyName();
// Estraven
seat2.sayMyName();
// Genly Ai
etc.
}
var passenger1 = "Estraven";
var passenger2 = "Genly Ai";
fly(passenger1, passenger2);
Upvotes: 34
Reputation: 12853
I'm still not happy with all these answers. They all start talking about "function declarations" and my monkey brain has already wandered off and started thinking about unicorns. That doesn't help me remember at all, it's just the definition.
I want something that I can immediately and forever hold in my head.
The only answer here that I quickly understand is:
"Arguments are actual values that are passed in".
Arguments are easier to define and as long as you know what they are then you know parameters are the other.
The other way I can think of it is:
Simplified down to:
Arguments outside, parameters inside
If any one wants to disagree with me, you can leave your arguments outside ;)
Upvotes: 5
Reputation:
A parameter is a variable in the declaration of the function.
An argument is the actual value of the variable that gets passed to the function.
Upvotes: 226
Reputation: 23572
Parameter is the variable in the declaration of the function.
Argument is the actual value of this variable that gets passed to the function.
Upvotes: 514
Reputation: 458
Consider the below java code.
public class Test{
public String hello(String name){
return "Hello Mr."+name;
}
public static void main(String args[]){
Test test = new Test();
String myName = "James Bond";
test.hello(myName);
}
}
The method definition of hello(String name) declares a String parameter called name. In the main method we are calling the hello method by passing the argument myName.
So parameter is the placeholder where as argument is the actual value for a method.
Upvotes: 0
Reputation: 31
Always Remember that:
Arguments are passed while parameters are received.
Upvotes: 15
Reputation: 73263
Or maybe it's even simpler to remember like this, in case of optional arguments for a method:
public void Method(string parameter = "argument")
{
}
parameter
is the parameter, its value, "argument"
is the argument :)
Upvotes: 14
Reputation: 29
This is a key:value issue...
The parameter is the key
The argument is the value
/****************************************/
Example:
name: "Peter"
/********/
let printName = (name) => console.log(name)
printName("Peter")
/********/
In this case, the parameter is "name", the argument is "Peter"
Upvotes: -2
Reputation: 1988
The parameters of a function/method describe to you the values that it uses to calculate its result.
The arguments of a function are the values assigned to these parameters during a particular call of the function/method.
Upvotes: 11
Reputation: 15602
Generally speaking, the terms parameter and argument are used interchangeably to mean information that is passed into a function.
Yet, from a function's perspective:
Upvotes: 18
Reputation: 15062
As my background and main environment is C, I will provide some statements/citations to that topic from the actual C standard and an important reference book, from also one of the developers of C, which is often cited and common treated as the first unofficial standard of C:
The C Programming Language (2nd Edition) by Brian W. Kernighan and Dennis M. Ritchie (April 1988):
Page 25, Section 1.7 - Functions
We will generally use parameter for a variable named in the parenthesized list in a function definition, and argument for the value used in the call of the function. The terms formal argument and actual argument are sometimes used for the same distinction.
ISO/IEC 9899:2018 (C18):
3.3
argument
actual argument
DEPRECATED: actual parameter
expression in the comma-separated list bounded by the parentheses in a function call expression, or a sequence of preprocessing tokens in the comma-separated list bounded by the parentheses in a function-like macro invocation.
3.16
parameter
formal parameter
DEPRECATED: formal argument
object declared as part of a function declaration or definition that acquires a value on entry to the function, or an identifier from the comma-separated list bounded by the parentheses immediately following the macro name in a function-like macro definition.
Upvotes: 3
Reputation: 7265
The use of the terms parameters and arguments have been misused somewhat among programmers and even authors. When dealing with methods, the term parameter is used to identify the placeholders in the method signature, whereas the term arguments are the actual values that you pass in to the method.
MCSD Cerfification Toolkit (Exam 70-483) Programming in C#, 1st edition, Wrox, 2013
Real-world case scenario
// Define a method with two parameters
int Sum(int num1, int num2)
{
return num1 + num2;
}
// Call the method using two arguments
var ret = Sum(2, 3);
Upvotes: 23
Reputation: 35695
A "parameter" is a very general, broad thing, but an "argument: is a very specific, concrete thing. This is best illustrated via everyday examples:
Most machines take an input and return an output. For example a vending machine takes as an input: money, and returns: fizzy drinks as the output. In that particular case, it accepts as a parameter: money.
What then is the argument? Well if I put $2.00 into the machine, then the argument is: $2.00 - it is the very specific input used.
Let's consider a car: they accept petrol (unleaded gasoline) as an input. It can be said that these machines accept parameters of type: petrol. The argument would be the exact and concrete input I put into my car. e.g. In my case, the argument would be: 40 litres of unleaded petrol/gasoline.
An argument is a particular and specific example of an input. Suppose my machine takes a person as an input and turns them into someone who isn't a liar.
What then is an argument? The argument will be the particular person who is actually put into the machine. e.g. if Colin Powell is put into the machine then the argument would be Colin Powell.
So the parameter would be a person as an abstract concept, but the argument would always be a particular person with a particular name who is put into the machine. The argument is specific and concrete.
That's the difference. Simple.
Post a comment and I'll fix up the explanation.
Upvotes: 6
Reputation: 3358
Parameter is a variable in a function definition
Argument is a value of parameter
<?php
/* define function */
function myFunction($parameter1, $parameter2)
{
echo "This is value of paramater 1: {$parameter1} <br />";
echo "This is value of paramater 2: {$parameter2} <br />";
}
/* call function with arguments*/
myFunction(1, 2);
?>
Upvotes: 3
Reputation: 73
You need to get back to basics.Both constructors and methods have parameters and arguments.Many people even call constructors special kind of methods.This is how a method is declared parameters are used:
type name(parameters){
//body of method
}
And this is how a constructor is declared parameters are used:
classname(parameters){
//body
}
Now lets see an example code using which we calculate the volume of a cube:
public class cuboid {
double width;
double height;
double depth;
cuboid(double w,double h,double d) {
//Here w,h and d are parameters of constructor
this.width=w;
this.height=h;
this.depth=d;
}
public double volume() {
double v;
v=width*height*depth;
return v;
}
public static void main(String args[]){
cuboid c1=new cuboid(10,20,30);
//Here 10,20 and 30 are arguments of a constructor
double vol;
vol=c1.volume();
System.out.println("Volume is:"+vol);
}
}
So now you understand that when we call a constructor/method on an object at some place later in the code we pass arguments and not parameters.Hence parameters are limited to the place where the logical object is defined but arguments come into play when a physical object gets actually created.
Upvotes: 1
Reputation: 22113
It's explained perfectly in Parameter (computer programming) - Wikipedia
Loosely, a parameter is a type, and an argument is an instance.
In the function definition f(x) = x*x
the variable x
is a parameter; in the function call f(2)
the value ``2 is the argument of the function.
In computer programming, two notions of parameter are commonly used, and are referred to as parameters and arguments—or more formally as a formal parameter and an actual parameter.
For example, in the definition of a function such as
y = f(x) = x + 2,
x is the formal parameter (the parameter) of the defined function.
When the function is evaluated for a given value, as in
f(3): or, y = f(3) = 3 + 2 = 5,
is the actual parameter (the argument) for evaluation by the defined function; it is a given value (actual value) that is substituted for the formal parameter of the defined function. (In casual usage the terms parameter and argument might inadvertently be interchanged, and thereby used incorrectly.)
Upvotes: 3
Reputation: 2333
According to Joseph's Alabahari book "C# in a Nutshell" (C# 7.0, p. 49) :
static void Foo (int x)
{
x = x + 1; // When you're talking in context of this method x is parameter
Console.WriteLine (x);
}
static void Main()
{
Foo (8); // an argument of 8.
// When you're talking from the outer scope point of view
}
In some human languages (afaik Italian, Russian) synonyms are widely used for these terms.
In my university professors use both kind of names.
Upvotes: 3
Reputation: 1
I thought it through and realized my previous answer was wrong. Here's a much better definition
{Imagine a carton of eggs: A pack of sausage links: And a maid } These represent elements of a Function needed for preparation called : (use any name: Lets say Cooking is the name of my function).
A Maid is a method .
( You must __call_ or ask this method to make breakfast)(The act of making breakfast is a Function called Cooking)_
Eggs and sausages are Parameters :
(because the number of eggs and the number of sausages you want to eat is __variable_ .)_
Your decision is an Argument :
It represents the __Value_ of the chosen number of eggs and/or sausages you are Cooking ._
{Mnemonic}
_" When you call the maid and ask her to make breakfast, she __argues_ with you about how many eggs and sausages you should eating. She's concerned about your cholesterol" __
( Arguments , then, are the values for the combination of Parameters you have declared and decided to pass to your Function )
Upvotes: 2
Reputation: 7157
Simple:
Upvotes: 212
Reputation: 115
Parameters are variables that are used to store the data that's passed into a function for the function to use. Arguments are the actual data that's passed into a function when it is invoked:
// x and y are parameters in this function declaration
function add(x, y) {
// function body
var sum = x + y;
return sum; // return statement
}
// 1 and 2 are passed into the function as arguments
var sum = add(1, 2);
Upvotes: 1
Reputation: 8308
Yes! Parameters and Arguments have different meanings, which can be easily explained as follows:
Function Parameters are the names listed in the function definition.
Function Arguments are the real values passed to (and received by) the function.
Upvotes: 6
Reputation: 1
The formal parameters for a function are listed in the function declaration and are used in the body of the function definition. A formal parameter (of any sort) is a kind of blank or placeholder that is filled in with something when the function is called.
An argument is something that is used to fill in a formal parameter. When you write down a function call, the arguments are listed in parentheses after the function name. When the function call is executed, the arguments are plugged in for the formal parameters.
The terms call-by-value and call-by-reference refer to the mechanism that is used in the plugging-in process. In the call-by-value method only the value of the argument is used. In this call-by-value mechanism, the formal parameter is a local variable that is initialized to the value of the corresponding argument. In the call-by-reference mechanism the argument is a variable and the entire variable is used. In the call- by-reference mechanism the argument variable is substituted for the formal parameter so that any change that is made to the formal parameter is actually made to the argument variable.
Upvotes: 0
Reputation: 404
Logically speaking,we're actually talking about the same thing. But I think a simple metaphor would be helpful to solve this dilemma.
If the metaphors can be called various connection point we can equate them to plug points on a wall. In this case we can consider parameters and arguments as follows;
Parameters are the sockets of the plug-point which may take various different shapes. But only certain types of plugs fit them.
Arguments will be the actual plugs that would be plugged into the plug points/sockets to activate certain equipments.
Upvotes: 4
Reputation: 20921
Parameters and Arguments
All the different terms that have to do with parameters and arguments can be confusing. However, if you keep a few simple points in mind, you will be able to easily handle these terms.
- The formal parameters for a function are listed in the function declaration and are used in the body of the function definition. A formal parameter (of any sort) is a kind of blank or placeholder that is filled in with something when the function is called.
- An argument is something that is used to fill in a formal parameter. When you write down a function call, the arguments are listed in parentheses after the function name. When the function call is executed, the arguments are plugged in for the formal parameters.
- The terms call-by-value and call-by-reference refer to the mechanism that is used in the plugging-in process. In the call-by-value method only the value of the argument is used. In this call-by-value mechanism, the formal parameter is a local variable that is initialized to the value of the corresponding argument. In the call-by-reference mechanism the argument is a variable and the entire variable is used. In the call- by-reference mechanism the argument variable is substituted for the formal parameter so that any change that is made to the formal parameter is actually made to the argument variable.
Source: Absolute C++, Walter Savitch
That is,
Upvotes: 9
Reputation: 837
Parameters are the variables received by a function.Hence they are visible in function declaration.They contain the variable name with their data type. Arguments are actual values which are passed to another function. thats why we can see them in function call. They are just values without their datatype
Upvotes: 1
Reputation: 1
When we create the method (function) in Java, the method like this..
data-type name of the method (data-type variable-name)
In the parenthesis, these are the parameters, and when we call the method (function) we pass the value of this parameter, which are called the arguments.
Upvotes: 3
Reputation: 1
Oracle's Java tutorials define this distinction thusly: "Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order."
A more detailed discussion of parameters and arguments: https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
Upvotes: 4