Public Group

# PROTOTYPES

This topic is 4542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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.

now i understand

##### Share on other sites
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 on other sites
Because a name must be declare before use.

##### 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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 23
• 9
• 56
• ### Forum Statistics

• Total Topics
632638
• Total Posts
3007601
• ### Who's Online (See full list)

There are no registered users currently online

×