Why do we declare and define functions separately in C++? [duplicate]
Asked Answered
B

3

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?

Brooksbrookshire answered 12/9, 2019 at 11:28 Comment(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.Gastrotrich
G
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.

Goodhumored answered 12/9, 2019 at 11:40 Comment(1)
Thank you for your time commenting. I need more experience to understand all this, nonetheless I greatly appreciate your time and help.Brooksbrookshire
B
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.

Banter answered 12/9, 2019 at 11:38 Comment(5)
This helps a lot. I definitely need more experience to fully understand your explanation, but I appreciate your commend. Thank you.Brooksbrookshire
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 ??Sudatory
@NaderBelal As long as it isn't necessary, separating declaration from definition is just overhead. WIP has little to do with that.Banter
When could it be necessary ??Sudatory
@NaderBelal Mutual recursion and headers.Banter
K
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.

Koziel answered 12/9, 2019 at 11:33 Comment(10)
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.Tanguy
What do you mean?Koziel
He asked why they are separated, i believe this answers the questionKoziel
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.Palocz
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.Tanguy
@Morpheus it increases code reuse. It reduces duplication.Palocz
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.Koziel
@Morpheus nope. Code reuse is good! You avoid duplication by reusing code.Palocz
What is the different between headers, interfaces and abstract classes?Koziel
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 questionKoziel

© 2022 - 2024 — McMap. All rights reserved.