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?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
jshji
  • 196
  • 1
  • 8
  • 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. – Boann Sep 12 '19 at 16:28

3 Answers3

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you for your time commenting. I need more experience to understand all this, nonetheless I greatly appreciate your time and help. – jshji Sep 12 '19 at 13:16
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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
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.

  • 1
    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. – Fureeish Sep 12 '19 at 11:34
  • What do you mean? –  Sep 12 '19 at 11:35
  • He asked why they are separated, i believe this answers the question –  Sep 12 '19 at 11:36
  • The gist of the top answer to [this question](https://stackoverflow.com/questions/873745/why-put-a-class-declaration-and-definition-in-two-separate-files-in-c) is as valid for functions as it is for classes, if you want to complement Morpheus' answer. – Federico klez Culloca Sep 12 '19 at 11:38
  • 1
    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. – Fureeish Sep 12 '19 at 11:38
  • @Morpheus it *increases* code reuse. It reduces *duplication*. – Federico klez Culloca Sep 12 '19 at 11:43
  • thats what i meant. What i mean it is help to make sure you are not unnecessarily using code over and over is another way of saying avoid duplication. –  Sep 12 '19 at 11:43
  • @Morpheus nope. Code reuse is good! You avoid duplication by reusing code. – Federico klez Culloca Sep 12 '19 at 11:44
  • What is the different between headers, interfaces and abstract classes? –  Sep 12 '19 at 11:45
  • i would like to make a point. I answered the question in its context. the fact you can have have declaration and implementation is the same file is irrelevant to the question –  Sep 20 '19 at 12:51