wakey
wakey

Reputation: 2409

Does using a prototype + definition instead of just a definition speed up the program?

Im very new to C programing, but have limited experience with Python, Java, and Perl. I was just wondering what the advantage is of having the prototype of a function above main() and the definition of that function below main() rather than just having the definition of said function above main(). From what I have read, the definition can act as a prototype as well.

Thanks in advance,

Upvotes: 1

Views: 119

Answers (4)

AnT stands with Russia
AnT stands with Russia

Reputation: 320699

There's no advantage in splitting a function into a standalone prototype and a definition. In fact, there's a clear and explicit disadvantage to that approach, since it requires extra maintenance efforts to keep the prototype and the definition in perfect sync. For this reason, a reasonable approach would be to provide a separate prototype only when you have to. For example, with external functions declared in header files you have no other choice but to keep the prototype and definition separate.

As for functions internal to a single translation unit (the ones that we'd normally declare static) there's absolutely no reason to create a standalone prototype, since the definition of the function itself includes a prototype. Just define your functions in the natural order: low level functions first, high level functions last, and that's it. If your program consists of a single translation unit, the main function will end up at the very bottom.

The only case you'd have to declare a standalone prototype in this approach would be if some form of recursion was involved.

Upvotes: 2

Jay
Jay

Reputation: 14471

If you write your code in the correct order, i.e. main last, then there is only one definition in your code for each function. This prevents having to change the code in two, or more, places if changes ever need to be made. That prevents errors and reduces the work to maintain it. The principle is called "single source of truth"

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 994231

The use of a prototype above main() (within a single module) is mostly a matter of personal preference. Some people like to see main() at the bottom of the file; other people like to see it at the top. I had a professor in university who complained that I wrote my programs "upside-down" because I put main() at the bottom (and avoided having to write and maintain prototypes for everything).

There is one situation where a prototype may be required:

void b(); // prototype required here

void a()
{
    b();
}

void b()
{
    a();
}

In this mutually recursive situation, you need at least one of the prototypes to appear prior to the definition of the other function.

Upvotes: 6

user541686
user541686

Reputation: 210735

  • When someone else wants to use your library, you can give them the header (containing the prototypes), without giving them your code.

    Example? Windows SDK.

  • If two functions call each other, you will need to have a prototype for at least one of them.

There is no performance impact* on the compiled code.

*Note: Well, there could be, I guess. If the compiler decides to put the method somewhere else in the binary, then you could theoretically see locality issues affecting the performance of your application. It's not something to normally worry about, though.

Upvotes: 3

Related Questions