why do we use function definition in header file and function declaration in obj
why can,t both be at header file?

This is because when you have several modules, you don't want the code to be in more than one place.

The #include directive essentially copies the contents of the file into your source module. If you have 5 modules that each include a header that contains actual code, you will have 5 individual copies of that same code. That might work fine for compiling each module, but the linker will complain about which of the 5 you may have meant.

The function declaration (prototype) can exist in the header, be included by many modules, and only tells the compiler the function "signature" -- whether or not it is being used correctly in each module. The function definition can only exist once.

[Note that you have definition and declaration backwards.]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.