+ 1
Is function declaration(prototype) nessassary in c++?? When with definition we can do.
In Javascript, php, we defines function and calls function, but in c++ first we have to write function declaration(prototype) than definition, and than can call, is it nessassary to write prototype in c++??
3 Respostas
+ 3
No, you can just immediately define the function, e.g.
void swap( int& left, int& right )
{
// ...
}
without writing
void swap( int&, int& );
first.
Splitting it up into declaration and definition is typically done when working with header (.h) and implementation (.cpp) files, where the header files contain the declarations and the implementation files the definitions.
+ 1
It is suggested in MISRA 2012 so It is better to give prototypes every function.