I know that function prototypes are useful for avoiding things like compilation errors, but are there any other good reasons to use function prototypes?
Printable View
I know that function prototypes are useful for avoiding things like compilation errors, but are there any other good reasons to use function prototypes?
Ummm....why wouldn't you use them?
If you didn't have function prototypes, you'd have to define every single function above main(). That's fine if you've only got three or four, but it would completely break the purpose of .h files.
The point of separating definition from declaration is so that code which uses the interface of a function doesn't need to be re-compiled if the definition of the function changes (although it will need to be re-linked). You can't do that without prototypes.
No.
Just wondering why many beginners think that removing compiler errors is all about programming!
Every language has set of rules (like English has), and you must follow the rules. Compiler and Linker need information about your program code, so you must provide them the way they need.
A global variable referenced in multiple CPP files is an example where you need to use 'extern' to tell the compiler. The (only) non-extern declaration will tell about variable to linker. It's not all about removing errors.
That sounds like a pretty good reason to me.Quote:
Originally Posted by FrankyBones
Well, there are others too. One is for the sake of good coding standards, so that other programmers can understand the code better.Quote:
Originally Posted by 7stud
well function prototypes let you see what a file does just by looking at the header
if what your saying though is that the compiler/linker should assum the function is somewhere else in the file or in another file then scope issues could arrise or you would have to redefine the function which isn't allowed, where re-prototpying a function is allowed
In addition,
If we have two fucntions that each of them call the other, it won't be able to compile the code without having function prototyping.
several points are here:
first : declaring function prototype can make another checking on the function typing mistake, usually we find that the function prototype and the function implementation name is not the same.if we declare function prototype, when implementing it, we can simply copy the function name,and make the implemention below, it can make sure our typing correctness.
second: if not giving function prototype,u will have to make sure it is called after it is declared, or it will give one such error, some function is not defined.
regards,
jolley
You're missing the point. There is no need for function prototype declarations. The complier could quite easily plant all the necessary code to call a function without ever being given any information about that function. The linker will sort out the actual address to replace the placeholder inserted by the compiler.
What the function prototype gives is information to the compiler so that it can check at compile time that you are calling the function with the correct number, types and order of arguments. Function prototypes are not about avoiding compilation errors - they are about generating more of them, so that you get fewer run-time errors because of mis-called functions.
maybe, these points that i just give are based on personal favors alike,of course declaring function prototype is not a must if u have your own policy, but it has some advantages that i have listed, right? imo, i like declaring function prototype, just to make the code decent and more readable.Quote:
You're missing the point.
i mean it can make sure the typing correctness,and if we make the function prototype and the function implementation name different, don't we receive a compilation error?Quote:
Function prototypes are not about avoiding compilation errors
regards,
jolley.
How, exactly, does it make code decent (and I don't understand what you mean by "decent") and more readable?Quote:
Originally Posted by jolley
I was addressing the OP in my answer
I was stating the simple fact that function prototypes are not needed to avoid compliation errors. There are plenty of languages that work perfectly well without requiring you to declare a function prototype (or have the full function definition available) before you use them.Quote:
Originally Posted by original post
You've just made my point exactly: without a function prototype, then the incorrect function call would not generate a compilation error (*). With the prototype, it does. Hence, prototypes are about generating compilation errors (for incorrect usage), not about avoiding them.Quote:
Originally Posted by jolley
Don't get me wrong - I'm in favour of function prototypes, but the reason is that they catch incorrect usage errors early rather than late; and they do that by generating compilation errors, rather than linker or run-time errors.
(*) Yes, I know a C++ compiler will generate an error, but that's a semantic requirement of the language. There is no reason in principle, though, that any compiler has to issue an error if there is no prototype.
I made a test.
Note: The numbers and colons at the left of the source codes are not included(line numbers). It should be compiled without the numbers and colons.
Test1: NO PROTOTYPE, FUNCTION ABOVE main()
1: #include <iostream.h>
2: #include <stdlib.h>
3:
4: int Add(int FirstNumber, int SecondNumber) //the function
5: {
6: return (FirstNumber + SecondNumber);
7: }
8:
9: int main() //main()
10: {
11: int firstNum = 5, secondNum = 10, sum;
12: sum = Add(firstNum, secondNum);
13:
14: std::cout << "5 + 10 = " << sum << std::endl;
15:
16: system("pause");
17: return 0;
18: }
Test1 Result
-No error
Test2: NO PROTOTYPE, FUNCTION BELOW main()
1: #include <iostream.h>
2: #include <stdlib.h>
3:
4: int main() //main()
5: {
6: int firstNum = 5, secondNum = 10, sum;
7: sum = Add(firstNum, secondNum);
8:
9: std::cout << "5 + 10 = " << sum << std::endl;
10:
11: system("pause");
12: return 0;
13: }
14:
15: int Add(int FirstNumber, int SecondNumber) //the function
16: {
17: return (FirstNumber + SecondNumber);
18: }
Test2 Result
-Line 7: Call to undefined function
Test3: FUNCTION WITH PROTOTYPE BELOW main()
1: #include <iostream.h>
2: #include <stdlib.h>
3:
4: int Add(int FirstNumber, int SecondNumber); //the prototype
5:
6: int main() //main()
7: {
8: int firstNum = 5, secondNum = 10, sum;
9: sum = Add(firstNum, secondNum);
10:
11: std::cout << "5 + 10 = " << sum << std::endl;
12:
13: system("pause");
14: return 0;
15: }
16:
17: int Add(int FirstNumber, int SecondNumber) //the function
18: {
19: return (FirstNumber + SecondNumber);
20: }
Test3 Result
-No error
Test4: FUNCTION WITH PROTOTYPE ABOVE main()
1: #include <iostream.h>
2: #include <stdlib.h>
3:
4: int Add(int FirstNumber, int SecondNumber); //the prototype
5:
6: int Add(int FirstNumber, int SecondNumber) //the function
7: {
8: return (FirstNumber + SecondNumber);
9: }
10:
11: int main() //main()
12: {
13: int firstNum = 5, secondNum = 10, sum;
14: sum = Add(firstNum, secondNum);
15:
16: std::cout << "5 + 10 = " << sum << std::endl;
17:
18: system("pause");
19: return 0;
20: }
Test4 Result
-No error
There is no <iostream.h> on my compiler. The correct header is <iostream>, not <iostream.h>.Quote:
Originally Posted by richard_tominez
Also, CodeGuru postings contains things such as code tags. You should use them when posting code.
Regards,
Paul McKenzie
If you're talking about C++, then yes, the compilation errors will occur if you call a function that hasn't been declared. That declaration can come in the form of just a prototype, or the entire function body occuring before the call.Quote:
Originally Posted by FrankyBones
Now, if you go back to the genesis of why function prototypes came about, it came from the "second edition" of the C standard. The first cut of the C language didn't have function prototypes, so this code would compile, run, and produce unpredictable results.
This code is compilable by C. However, when you run it, expect weird things to happen, since the real sqrt() function takes a single double argument, and returns a double. Bugs like this existed like the plague in many 'C' programs back in the old days. So along come the language writers, and in the second cut of 'C', they came up with the prototype, so as to lessen these types of bugs.Code:int main()
{
char x = 'c';
int value = sqrt(x, 34);
}
Now the above code when compiled with a modern 'C' compiler will still not produce an error, since prototypes are still not mandatory in 'C'. However if a prototype is introduced, then the code will produce a compiler error.
Also on a side note --Code:#include <math.h>
int main()
{
char x = 'c';
int value = sqrt(x, 34); // now the C compiler will emit an error
}
notice how many times we emphasize that when posting code, to include all headers and prototypes that the code is referring to (this is especially the case for 'C' code). The reason is that if the code is 'C', not including a header is perfectly valid, but the results of running the code can be radically different.
Regards,
Paul McKenzie