Home:ALL Converter>Declaration or Definition

Declaration or Definition

Ask Time:2010-10-03T10:48:38         Author:Nivhus

Json Formatter

I have a confusion about declaration and definition.

In the statement

int switchon(float duration);

Q1. Is the parameter 'duration' being defined or is it being declared?

As per section 3.1/2 of the holy Standard, it is a definition, but I am unable to understand why.

Q2. What is the exact difference between declaration and definition?

C++ In a Nutshell says that

A definition defines the storage, value, body, or contents of a declaration. The difference between a declaration and a definition is that a declaration tells you an entity's name and the external view of the entity, such as an object's type or a function's parameters, and a definition provides the internal workings of the entity: the storage and initial value of an object, a function body, and so on.

This definition of 'definition and declaration' also does not help me to understand why 'duration' is a definition and not a declaration in the statement above.

REDIT:

UncleO's post gave me an idea and this is what I tried:

I changed my code as:

int switchon(float duration, int duration);   // idea is to see what error 
                                              // compiler gives

int main()  {  }

error C2371: 'duration' : redefinition; different basic types

Author:Nivhus,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/3848356/declaration-or-definition
UncleO :

Answer to Q1:\n\nThe identifier \"duration\" is not being defined nor declared. \"switchon\" is the identifier being declared in this statement, as a function with a float parameter returning an int.\n\nUsing \"duration\" here is optional, which makes it easier to cut and paste from the function definition elsewhere, but has no meaning.\n\nEDIT:\n\nWhat a nasty change!\n\nint switchon(float duration, int duration); // idea is to see what error compiler gives\n\nint main() { }\n\nerror C2371: 'duration' : redefinition; different basic types \n\n\nThis is a particular compiler that throws this error, so not necessarily definitive. This example does lead to bad code, but in my opinion, it shouldn't throw that particular error.\n\nOn the other hand,\n\nint switchon(float duration, int duration){ return 0; }\n\nint main() { }\n\n\nshould lead to the redefinition error given above. In this case, switchon is being defined, which means that float duration and int duration are defined as parameters in the same scope with the same name.\n\nIt may be that the compiler is being overzealous, or uses the same mechanism to parse the function in both cases. It's definitely an error in the second case and just a bad idea in the first case, so it's not a bad thing that it throws an error in both.\n\nAnswer to Q2: The difference is just as it says in the section, also explained in this duplicate",
2010-10-03T03:35:15
Bart van Ingen Schenau :

There is a difference in definition of the term definition between C and C++. This difference is not well known (I only found out about is when researching this question) and most people and books use the C definition also in the context of C++.\n\nIn C, a declaration is also a definition if it has <choose from set> extra properties.\nIn C++, a declaration is always a definition, unless it is one of <choose from set>.>br/>\nFor most practical purposes, this difference between the definitions of definition does not make any difference, until you start looking at the corner cases.\n\nIn C, a struct-member and a parameter in a function prototype are not definitions, because they don't have the required property of reserving memory for the object being declared.\nIn C++, a struct (or class) member-variable and a parameter in a prototype are definitions, because they don't fall within the list of exclusions.\n\nI think this difference is unneeded and unfortunate, because it makes it more difficult than needed to understand the difference between declaration and definition.\n\n\n Q1. Is the parameter 'duration' being defined or is it being declared?\n\n\nAccording to a literal reading of the C++ standard, it is being defined. But I hope this is a defect in the standard, because declared makes more sense to me.\n\n<quote>\n\nint switchon(float duration, int duration); // idea is to see what error \n // compiler gives\n\nint main() { }\n\n\n\nerror C2371: 'duration' : redefinition; different basic types\n\n\n\n</quote>\n\nMost likely, you will get the same error for:\n\nextern float duration;\nextern int duration;\n\n\nThese are both declarations, but the compiler diagnostic might not reflect that.",
2010-10-04T14:11:47
John Percival Hackworth :

The line:\n\nint switchon(float duration);\n\n\nis a declaration because you are specifying the signature for the function. The name of the formal parameter is optional at this point.\n\nA1: The fact that you have a parameter is being declared.\n\nA2: A declaration is where you specify the function signature, while the definition is where you to specify implementation and the names of the formal parameters.",
2010-10-03T02:57:17
0x1F602 :

I guess I learned that I'm wrong, I too had the two confused. Anyhow, it is not given a value here. You give it a value when you call the function.\n\nFor example, tell me what duration's value/contents is based on this example alone? You can't. Therefore, it is a declaration.",
2010-10-03T02:57:58
emrea :

Q1: It is a declaration because an actual implementation was not provided for the function \"switchon\". If it were sth like: \n\n\nint switchon(float duration) {\n // do sth with duration\n return result;\n}\n\n\nThen, it would have been a definition (b/c all details are provided). \n\nQ2: A declaration gives only a signature whereas a definition defines what the function actually does.",
2010-10-03T02:58:29
yy