Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different declaration and definition in c++

I'm a bit hazy on the rules of declarations vs. definitions.

I have the following declaration in funcs.h:

void sumTotalEnrgyAndClush(Protein &A,Protein &B,double ans[2],double enrgyA[18][18],double     enrgyB[18][18]);

Notice that ans[2] is before enrgyA and B.

In the funcs.cpp file the definition starts like this:

void sumTotalEnrgyAndClush(Protein &A,Protein &B,double enrgyA[18][18],double enrgyB[18][18],double ans[2])

It compiles (via makefile) and works fine.

I also noticed that if I remove the declaration the compiler seems to manage just fine.

Why doesn't the change in the order of the arguments matter? Is it that the last 3 items are all pointers so the difference in order doesn't matter?

like image 932
Meir Avatar asked Aug 26 '10 14:08

Meir


People also ask

What is difference between declaration and definition in C?

Declaration means that variable is only declared and memory is allocated, but no value is set. However, definition means the variables has been initialized. The same works for variables, arrays, collections, etc.

What is define declaration in C?

A declaration is a C language construct that introduces one or more identifiers into the program and specifies their meaning and properties. Declarations may appear in any scope.


1 Answers

Why doesn't the change in the order of the arguments matter?

The order does matter. In C++, functions can be overloaded, so two or more functions can have the same name if they have different parameters (or, if they are member functions, if they differ in const-qualification).

You have actually declared two sumTotalEnrgyAndClush functions. The declaration in the header file declares one function that is never defined, and the declaration in the source file declares and defines a second function.

If you tried to use the function declared in the header file (by calling it or taking its address, for example), you would get an error because that function is not defined.

like image 177
James McNellis Avatar answered Sep 30 '22 15:09

James McNellis