What is the point of prototyping a function?

I follow the curse guide, and all of the C code in the prototypes functions before main(), and then defines them later. In my research in C ++, I heard about prototyping functions, but I never did, and as far as I know, it doesn't make too much difference in how the code compiles. Is this a personal choice of a programmer more than anything else? If so, why was it included in C at all?

+5
source share
5 answers

In C, prototyping is required so that your program knows that you have a function with a name x()when you have not received its definition, so that it y()knows what exists and exists x(). C does top-down compilation, so it needs to be defined before the hand is a short answer.

x();
y();
main(){

}

y(){
x();
}

x(){
...
more code ...
maybe even y();
}
+3
source

The prototype of the function was not originally included in C. When you called the function, the compiler simply took your word for it to exist and took the type of arguments you provided. If you have an order of arguments, a number or the wrong type, too bad - your code will fail, perhaps mysteriously, at runtime.

C- . , . varargs , .

, C ( ++) foo_t func() , foo_t func(void). , . .

+11

, .h , , ( ).

, / .

+1

- . , .

C , , : . , , , , .

, , , . O (n) - ( , O (m), O (m * n)), n - . , , .

, , , , .

C (, , ++) .

+1

, , , .h, . , , say "getIterator()", , , ( , , ..).

, . , , .

There are ways to get around it as with a precompiled header, but in my opinion it is less elegant and has many drawbacks. From kurmiza it is C ++, not C. However, in practice, you may have a situation in which you would like to organize the code in such a way, classes aside.

0
source

All Articles