Sign in to follow this  
mc30900

PROTOTYPES

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
*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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this