+ 2

Function Prototype Example?

I've been struggling to understand this concept for a while, I know it has been asked before but I still need clarification. What I've gathered from research and posts are: 1) A function prototype can be used to declare a function without it's body first-hand letting the compiler know what arguments it takes, the function name and its return type. I don't see the point of letting the compiler know only about the header of the function, when you can let it know all about it at once. 2) Projects usually consists of multiple files and function prototypes let the compiler know it exists in another file. Doesn't it still work the same way even without a function prototype? Is it just good practice? 3) Gives the ability to define and implement functions after it is used(below). This point seemed to only give code flexibility. What I need is an example where a function prototype is absolutely necessary and in the case of omitting it, the program fails to work. Thanks in advance

31st Oct 2018, 9:31 AM
jtrh
jtrh - avatar
4 Antworten
+ 6
The reason for such feature in C/C++ is not something like a mandatory rule written on a monolith! If you believe in good programming practice and software engineering principles, then you should know this one important fact about the "least privilege principle/encapsulation the underlying implementation from the user". It's absolutely unnecessary, for example, for a library user to know about how the member functions of a particular class has been implemented -- knowing the required args and return type is sufficient. That's why there are a whole bunch of documentations out there with this format for example function prototype: int isupper(int c) return type: a non-zero integer for true and zero for false arguments: a single character description: this character classification function tests for any character that is an upper case. Most importantly, when the librarian changes/improves the function implementation, the interface stays intact. And that's a big + for consistency of usage from the user's POV.
31st Oct 2018, 10:07 AM
Babak
Babak - avatar
+ 5
jtrh I think my previous response was enough to see the point, but if you still need a complete example, that's A-OK For example: Reimplement the character classification functions using OOP paradigm. // header.h including all functions' interface(prototype) #pragma(once) class Ctype { public: static bool _isupper(int c); static bool _islower(int c); static bool _isalpha(int c); //... }; // implementation.cpp or a dll #include "header.h" bool Ctype::isupper(int c) { return (c >= 'A' && c <= 'Z') ? true : false; } //... // main.cpp #include "header.h" int main() { char c = 'X'; If (Ctype::isupper(c)) // do something }
31st Oct 2018, 10:49 AM
Babak
Babak - avatar
+ 2
C++ Soldier (Babak) this clears things up, thanks!
31st Oct 2018, 12:55 PM
jtrh
jtrh - avatar
+ 1
C++ Soldier (Babak) thanks for the reply. I've seen this in alot of other concepts, sometimes I get puzzled by such implementations because I see to things too straightforward. I would still like an example where a function prototype would be of good use, thanks!
31st Oct 2018, 10:16 AM
jtrh
jtrh - avatar