Is function declaration(prototype) nessassary in c++?? When with definition we can do. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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++??

12th Mar 2021, 6:36 PM
G. Yaseen
G. Yaseen - avatar
2 Réponses
+ 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.
12th Mar 2021, 8:27 PM
Shadow
Shadow - avatar
+ 1
It is suggested in MISRA 2012 so It is better to give prototypes every function.
12th Mar 2021, 10:35 PM
Emre İRİŞ
Emre İRİŞ - avatar