12

I have only just started learning C++, and I see that functions are usually declared and defined separately, for example:

// Declaration void sayhi(std::string name); // Definition void sayhi(std::string name) { std::cout << "Hello, " << name; } 

I tried looking up but most of the questions were for the cases of Class, but my question is in more general terms, why do we separate them? What's the benefit?

1
  • It used to be necessary on the ancient computers that C was originally used on. There's no good reason for it in new programming languages. Commented Sep 12, 2019 at 16:28

3 Answers 3

11

The same function can be used in different compilation units.

If it will be defined in a header and it is not an inline function or a function with the internal linkage then the One Definition Rule (ODR) will be broken provided that the header in included in several compilation units.

So usually such functions are declared in headers but defined in some modules. So using the header different compilation units will see the functions declarations and the functions will be defined only once.

If a program consists only from one compilation unit then there is no need to declare and define a function separatly because a function definition is at the same time its declaration.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your time commenting. I need more experience to understand all this, nonetheless I greatly appreciate your time and help.
9

why do we separate them?

We don't.
As long as we can get away with it at least, as it violated DRY, introducing (only partially checked) repetition.

The problem is that C comes from a long line of single-pass compilers, and while C++ bolted lots of things on with templates and return-type-deduction, it didn't quite reverse that fact.

Thus, if you want to use a function before its definition, you have to provide a forward-declaration.

And if you want to use separate compilation for parts of your code, which is generally advisable for shorter compile-times and ability to use libraries (static or not) in other languages, without sources, or compiled with other options, you need some way to tell the compiler what will be there.

Header-files are collections of such forward-declarations, constant-declarations, inline-functions (inline-functions must be defined in every translation unit using them), type-definitions and the like.
Generally, implementation-files include the corresponding hesders first to verify they work and are self-contained.

Admittedly, the module system introduced with C++20 is a new twist and further reduces the need for forward-declarations.

5 Comments

This helps a lot. I definitely need more experience to fully understand your explanation, but I appreciate your commend. Thank you.
To get it straight ... we usually don't separate function declaration and definition since it made script verbose, unless the program is WIP as it's still in the development phase ??
@NaderBelal As long as it isn't necessary, separating declaration from definition is just overhead. WIP has little to do with that.
When could it be necessary ??
@NaderBelal Mutual recursion and headers.
2

A header file is a convenient mechanism to access declarations of variables and function prototypes from multiple translation units.

#include <header> 

lets you include these in many classes. Thus make code more reusable.

10 Comments

One could implement everything in header files where you have no separation for declaration and definition. I believe this answer does not answer the question.
What do you mean?
He asked why they are separated, i believe this answers the question
The gist of the top answer to this question is as valid for functions as it is for classes, if you want to complement Morpheus' answer.
Your answer talks about separation of code to header files that can be included. This has nothing to do with separation of declaration and definition, e.g. .hpp and .cpp files. As I said - one could move some code to header files (like you say in your answer) and still not separate the declarations and definitions, which seems to be the essence of the question.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.