Function declaration and implementation

According to my teacher, this is a bad practice for writing custom functions like this:

int DoubleNumber(int Number)
{
    return Number * 2;
}

int main()
{
    cout << DoubleNumber(8);
}

Instead, he says that he always uses forward declarations, even if the functions do not need any knowledge of each other:

int DoubleNumber(int Number); // Forward declaration.

int main()
{
    cout << DoubleNumber(8);
}

int DoubleNumber(int Number) // Implementation.
{
    return Number * 2;
}

I find this particularly strange, as he told us how important it is that the declaration and implementation forward are exactly the same, or you get errors. If this is so important, why not just put everything higher main()?

So, is it really bad practice to declare and implement at the same time? Does it even matter?

+3
source share
9 answers

( "" ), , , , . , , - ( , - ).

+9

. , . , , (.. ). , ; .

+6

, - C.

C , , ( ), , , , .

, , . ; , , .

C ( ). - , GCC, , .

++ , ​​ . , C++ .

+6

. , .

, . custom-math.h custom-math.cpp, custom-math.h forward custom-math.cpp . . "" / .

, main(), , , , . main() , . .

+4
: " , . , , (.. " ). , . " " , . , , / . , PEBCAK.

PEBCAK, , , , .

, , , : (1) ( 2) .

hth.,

+3

, ;) , , , . - , , .

+2

, - , , , .h.

, , ( ), , . , . - :

void Func1() { ... }
...
void Func2() { ... }
...
void Func3() { ... }
...
int main() { Func1(); Func2(); Func3(); return 0; }

- , main() - , .

+1

. api, . , , cpp. , , , , . , cpp, , api. , , , .

+1

Personally, I only like to forward an ad that knows the client code (i.e. in the class header). Everything that is local to the class implementation (for example, helper functions) must be defined before use.

Of course, at the end of the day, for the most part, it comes down to the personal preferences or coding standard that follow your project.

0
source

Source: https://habr.com/ru/post/1779247/


All Articles