views:

202

answers:

6

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

A: 

The line:

int switchon(float duration);

is a declaration because you are specifying the signature for the function. The name of the formal parameter is optional at this point.

A1: The fact that you have a parameter is being declared.

A2: 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.

John Percival Hackworth
A: 

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.

For example, tell me what duration's value/contents is based on this example alone? You can't. Therefore, it is a declaration.

0x90
Yes, I also thought so. But my understanding of 3.1 is that this is a definition. I am unable to understand why
Nivhus
A: 

Q1: It is a declaration because an actual implementation was not provided for the function "switchon". If it were sth like:

int switchon(float duration) { // do sth with duration return result; }

Then, it would have been a definition (b/c all details are provided).

Q2: A declaration gives only a signature whereas a definition defines what the function actually does.

eakbas
A: 

Answer to Q1:

The 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.

Using "duration" here is optional, which makes it easier to cut and paste from the function definition elsewhere, but has no meaning.

EDIT:

What a nasty change!

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

int main() { }

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

This 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.

On the other hand,

int switchon(float duration, int duration){ return 0; }

int main() { }

should 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.

It 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.

Answer to Q2: The difference is just as it says in the section, also explained in this duplicate

UncleO
A: 
int switchon(float duration);

is a function prototype / declaration, and merely clarifies the order & type of arguments passed to the function as well as its return value type.

It is necessary only in cases where the entire function definition (ie. what the program does when the function is called ) is placed after a call to switchon() in some other function, most probably main().

In this regard, all variable names used in a function prototype are just placeholders, and can be safely omitted. So, your statement could just as well read,

int switchon(float);

Since the variable name is optional, this merely boils down to a function declaration, as per the standard's definition (no puns intended).

It is for this purpose that some textbooks would refer to duration in the your first prototype as a dummy variable.

Kedar Soparkar
A: 

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++.

In C, a declaration is also a definition if it has <choose from set> extra properties.
In C++, a declaration is always a definition, unless it is one of <choose from set>.>br/> For most practical purposes, this difference between the definitions of definition does not make any difference, until you start looking at the corner cases.

In 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.
In 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.

I think this difference is unneeded and unfortunate, because it makes it more difficult than needed to understand the difference between declaration and definition.

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

According 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.

<quote>

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

int main()  {  }
error C2371: 'duration' : redefinition; different basic types

</quote>

Most likely, you will get the same error for:

extern float duration;
extern int duration;

These are both declarations, but the compiler diagnostic might not reflect that.

Bart van Ingen Schenau