473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

function prototype

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
6 2154
Silent1Mezzo
208 100+
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
9,208 Expert Mod 8TB
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
Silent1Mezzo
208 100+
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
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
17
by: Matt Kruse | last post by:
Perl's map() function is a powerful shortcut to accomplish many "loop over an array and do X" operations. Below is a quick hack to simulate similar functionality. I've used it a few times and find...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
3
by: Jeff Stewart | last post by:
I've been working with the JavaScript Shell in Firefox on a mad-scientist problem I've had in my head. Assume a function named 'object' that is designed to create an object based on a prototype...
20
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
5
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
1
by: wkerplunk | last post by:
Below is what I have build with several different languages. It works great but I need help, I am stuck. When you click on an item in the dropdown autocomplete div it does a mousedown function...
2
by: hzgt9b | last post by:
I know how to overwrite a function. Normally this is what I would do: function someFunction() { /* orig definition here */ } //later in the execution stream I would do... someFunction = function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.