By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

function prototype

P: 34
In C/C++ do we really need to mention a function prototype everytime..

if the function is defined before its being called..do we need its prototype?
Jun 8 '07 #1
Share this Question
Share on Google+
6 Replies


100+
P: 208
In C/C++ do we really need to mention a function prototype everytime..

if the function is defined before its being called..do we need its prototype?
No..You don't need to have a prototype
Jun 8 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
No..You don't need to have a prototype
This is not correct.

All functions in C++ (except main() ) require a function prototype.

This is either a) the entire function is defined before the first call or b) the function is defined in another file, and the first line of the function followed by a semi-colon appears before the first call.

Unless you do this, your code will not compile.

This is not true in C.
Jun 8 '07 #3

100+
P: 208
This is not correct.

All functions in C++ (except main() ) require a function prototype.

This is either a) the entire function is defined before the first call or b) the function is defined in another file, and the first line of the function followed by a semi-colon appears before the first call.

Unless you do this, your code will not compile.

This is not true in C.
Sorry I was merely answering for C since I'm not a C++ programmer. I guess I should have specified that.
Jun 8 '07 #4

Banfa
Expert Mod 5K+
P: 8,916
This is not correct.

All functions in C++ (except main() ) require a function prototype.

This is either a) the entire function is defined before the first call or b) the function is defined in another file, and the first line of the function followed by a semi-colon appears before the first call.

Unless you do this, your code will not compile.

This is not true in C.
You have missed the case (that is quite similar to b) of the function is being call from a place in the file that is before it's definition in which case it requires a declaration(prototype) before it is called.


Also Note, today although it is not a strict requirement in C to call a function in the presence of a prototype it would be considered bad practice to do this and many C compilers will output a warning or message (but not an error) of some sort if you do this.
Jun 10 '07 #5

P: 93
In C/C++ do we really need to mention a function prototype everytime..

if the function is defined before its being called..do we need its prototype?
In C++ you have to put the prototypes in .h file and include the same
in .cpp file in which you should give the implementation.
Finally you can have the main in any other file, in which you will be calling
this function. As a good practice always put the function prototype.

Regards,
Girish
Jun 11 '07 #6

Banfa
Expert Mod 5K+
P: 8,916
In C++ you have to put the prototypes in .h file and include the same
in .cpp file in which you should give the implementation.
This is not true, the prototype can go anywhere including in a cpp file.

If you wanted to you could put the function in 1 cpp file and then separately put a prototype in every other cpp file you wish to call it from. However this would be extremely bad practice and asking for errors later in project if the function prototype got change.

The only stipulation is that a prototype has been seen before it is called. Good practice states that you should only have 1 prototype for any given function in your code, in a header file if that function is to be called from multiple other files or in the cpp file itself if it is a static function.

This is the difference between what you can do and what it is a good idea to do in order to produce maintainable code that is more likely to be bug free.

C++, like C before it, allows you to do all sorts of things that would be considered bad practice. It is up to the programmer to avoid doing them in order to increase the quality of their own code.

This is what a coding standard is about.
Jun 11 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.