PROTOTYPES

Started by
6 comments, last by Aardvajk 17 years, 12 months ago
hello all. i am reading tutorials right now but i have a question: what are PROTOTYPES?? i mean why do i have to write the function twice? thanks in advance
Advertisement
When you write a function the first time, before the main() function, you are declaring it. When you call it from inside main() or any other function, you are invoking it. When you write out the code for the function, you are defining it. All three are necessary for the code within the function to actually be executed. I've seen functions without prototypes anywhere called before, but I don't know how.
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
You don't have to write it twice. You only need prototypes if you try to reference a symbol (function, variable, etc) before it's deflared.

If you structure your code so that your functions are implemented higher up in the file before they're referenced then you don't need prototypes at all.

E.g.
void foo(){   // ...}int main(){   foo();    // Ok, foo is implemented before main   bar();    // Error, the compiler hasn't come across bar yet, doesn't know what it is   return 0;}void bar(){   // ...}

If you have a prototype of bar() before main(), then the compiler can know what the symbol is so it can use it.
*assuming C++, but should be similiar in other languages*
You don't need to prototype it, at least not always, but it is a good idea. The problem is that if you've got Foo() in FileA and Bar() in FileB, when the compiler compiles Foo(), it doesn't know that Bar() exists. Prototyping alows you to tell other file(or even other parts of the same file) about other functions. If you prototype Bar() at the top of FileA, you can now call Bar() from within Foo().

A better method would be to have a seperate header file, say header.h, that contains prototypes only, no declarations. Then, when you need to use that function, you just #include the header and you have the prototype.

And the best method would be to use classes and declare the type in a header and its functions in a source file.

So to sum it up, prototyping alows more parts of your code to access a function or class.
now i understand
thanks for you answers guys
prototypes are used to forward declare a function to ease(make possible) the linking
In C you can invoke a function defined in another module if and only if it's declared within the same file(or an included file) before the caller function
And if you want to use a function(in the same module) that has to be defined after the callee, you must declare it before the callee,too

Declaring is like :
"you must use this convention to call this function which I will define later in the code(same or another module"

Because a name must be declare before use.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Prototypes are mandatory in C++ if the function has not been defined before the point of call. They were optional in C but if not present, the compiler would turn off type checking for function parameters at the point of call and assume an int return value. This could cause a lot of subtle errors, hence C++ making prototypes mandatory.

This topic is closed to new replies.

Advertisement