[General] TIL the difference between a parameter and an argument

Other Language

I've always thought these were synonyms, but apparently they are not.

Parameters are the "variables" in the function declaration, while arguments are the values transferred via the parameters to the function when called. For example:

void f(int x) { ... }
f(3);

x is a parameter, and 3 is an argument.

Posted Jul 15, 2016, 9:33 AM UTC

21726
spcmrn•

i.e. a function has parameters and takes arguments.

relevant stackoverflow with useful mnemonics and more nice explanations

45
six_string_sensei•

TIL

13
CaptainBlagbird•

Error: Missing argument for TIL

15
SheeEttin•

That's not an argument!

2

Yes it is

10
myerscc•

or rather x is the formal parameter and 3 is the actual parameter. You could say the function in the code is parameterized by x - but usually the word "parameterized" is used for type parameters.

10
SpiderboydkOP•

Like in math. :-)

3
SheeEttin•

I feel like this is splitting hairs.

7
jalanb•

Which is why it is important

5
SheeEttin•

I mean I feel like it's an arbitrary distinction. Does it make a difference?

I'm guessing it might in certain languages, like C/C++, but I've never done anything complicated enough for it to matter.

4
ejmurra•

It doesn't make a difference in your code, you could call them floops and flarbs and your code would still work. It makes a difference when you are communicating to other devs about your code. Clear communication is key in teams so it is important to have a shared vocabulary and understanding of that vocabulary.

13
SuperFLEB•

But if most people don't know the difference, it's kind of a moot point.

7
agonnaz•

I had no idea; I've just been using them interchangeably.

4
patientdev•

Why is it called an argument? What's the usage history for that term in computer science?

4
desultoryquest•

Invoking functions probably led to a lot of developer arguments

3
LittleLui•

In the beginning, main was invoked. This has made a lot of people very angry and been widely regarded as a bad move.

9
desultoryquest•

Too bad the crt0 didn't learn from the dolphins and loop around itself doing precisely nothing instead of invoking main.

1
Jahishno•

It's also thought of as formal and actual parameters. Formal parameters are in the function declaration, actual parameters are what are given to the function call ie arguments.

3
some_q•

Interesting. Thanks for posting!

2
yes_or_gnome•

I don't feel like this is "general", or even precisely correct. Given the example, maybe this is standard vocabulary for C\C++, but from my experience (mostly interpreted languages), they're just synonyms. ... Or, possibly, IEEE or ISO has specific definitions.

As an example, a function has an "argspec" or a "parameter list". Since there is no type enforcement the spec is just list of names and, possibly, default values.

1
SpiderboydkOP•

I thought they were synonyms too until recently. Since many other languages also use the words "parameter" and "argument" I see no reason why this wouldn't apply there too. It probably also apply to math too.

7
Nebuleon•

I always like to think of functions as operations, like, say, cooking a meal, changing course on a plane, setting the phasers to something else etc.

An operation may have some parameters you need to set before starting it. But I always thought of that not as giving arguments, but as setting parameter values, which makes sense because you're actually setting the value of the variable that stands for the parameter inside the function.

Though, in C or C++, the word arg is used in describing what gets sent to a parameter of type ..., i.e. varargs. That may be a nice way to remember the difference, if you remember that parameters is the other thing.

1
agonnaz•

Actually, C calls it varargs, and the C++ varargs are a carryover from C. Proper C++ usage of ... (since C++11, before which you only had C-style varargs) is called a "parameter pack".

You definitely have to be pedantic about the difference between parameters and arguments in modern C++ with parameter packs, because they infer template type (and therefore parameter type) from the arguments given, with all sorts of rules and implications about what type the parameter is based on the argument's type, cv-qualification, reference type, etc.

2
m4lfuncti0n•

so, in short - an argument is the value of a function?

1
SpiderboydkOP•

It's the value(s) you pass to the function.

1