Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm a beginning programmer, trying to get a grasp on everything, so pardon the probably mundane theoretical question:

I see in a C++ tutorial that prototyping is needed to let the compiler know that a function(?) is coming up and not to throw up any errors. Why does it not search throughout a document to find the function it's looking for?

Maybe the question I'm asking is; what is the purpose for prototyping? And can it be used in other languages?

share|improve this question
    
Gotcha, I edited hopefully to get what I really meant across. –  Shawn Strickland Sep 9 '11 at 5:41
2  
I think a more common term is "forward declaration", since "prototyping" can also be a development model. –  oenone Sep 9 '11 at 8:14
1  
@oenone: Actually, the term is just "declaration", "forward declaration" is somewhat of a misnomer. –  sbi Sep 9 '11 at 8:53

3 Answers 3

up vote 7 down vote accepted

The reason is that you can use the function outside of the file where you implemented it. In this case searching throughout a document won't help because the compiler won't find it, yet it has to know the definitions of the function in order to enforce the typing and to encode the function name correctly for the linker to match it to the implementation.

It can be multilingual, C++ code can include direct calls to C functions (or any other language that adheres to C conventions when compiling the source code), which have to be declared as extern "C".

share|improve this answer
    
Thank you! Can the idea of prototyping be used in other languages in their correct syntax? –  Shawn Strickland Sep 9 '11 at 5:38
    
@Shawn - Can? Of course. C uses it, obviously. In ADA, Java and .NET languages you have to declare which packages you're going to use before actually using them. It's actually a common practice, because it's not too much a burden on the programmer, yet makes the compilers so much simpler and safer... –  littleadv Sep 9 '11 at 5:41
    
dislike the use of non-standard terms to refer to signature (definitions) and definition (implementation). this is quite confusing especially as you areguably use definitions in the wrong place –  jk. Sep 9 '11 at 11:32

Basically, it is because C++ is a statically typed language. That is, all types are fixed at compile-time, none resolved at run-time. (Some features, like virtual functions, somewhat weaken this principle, but do not invalidate it.)

In order to accept any identifier, be it a type, constant, variable, function, whatnot, the compiler needs to know the exact type of that identifier. That type is introduced by a declaration. The compiler uses this to check whether the identifier is used appropriately according to its type. This prevents you from incrementing a floating point variable, changing a constant, invoking a member function on an object of a class that doesn't have that member function, and call a function with the wrong number and type of arguments.

Later, the linker will link all references to a certain identifier to the definition of it. (So it has to be defined somewhere, and it has to be defined exactly once.) Now, every definition can be used in place of a declaration, so you could get away without declaring anything at all, if you defined everything before its use.

However, this does have serious drawbacks, the most obvious ones being that it would abandon separate compilation and leaves you no way to define two or more identifiers that reference each other.

share|improve this answer
    
This isn't really the reason. You could use implicit declarations and to some degree, that will even work (specifically in C). The reason why it isn't in the C++ standard is that deducing the type of an expression is very, very hard (although not impossible). –  Let_Me_Be Sep 9 '11 at 9:51

To support the "Header file" mechanism. This is applicable to C also. We implement functions, and their signatures or prototypes are exposed to external users through header files.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.