Where to put Functions in Code

Newbie Here,
I am trying to figure out if matters where we put the functions for our code, if I put above main(), then I don't have to call them out, if I put them below, I do have to declare them. I would assume that you would want to put functions before since it lessons the declarations needed, but I would like to know what everyone here thinks. I bet their is reasons to do both, so please share your thoughts on this.

Thanks again.
I'm going to assume you are talking about using prototypes or not. I will typically put functions above main() just because I've used python for a while now. It honestly doesn't matter and is more a matter of preference than anything (assuming they are all going into the same source file. across multiple source files its a bit different).
thanks nchambers, not using multiple files as of yet, thanks for the response. Good to know.
It really doesn't matter. Just remember this rule of thumb:

1. Be CONSISTENT when it comes to your style. Don't define functions without a forward declaration in one file and then use forward declarations in another. Forward declarations do little except introduce a type into th namespace, so you could even do both.

Take this file of mine as an example:

http://pastebin.com/TPcbeG3x
Last edited on
I can think of no reason to put them after main().
@millerizi

It sounds like you are talking about C style code, in which case this stuff applies.

If one puts function declarations before main, and function implementation code after main, then someone else reading the code doesn't have to go searching for the main function. It's easier if one doesn't have to scroll through 300 LOC to find main.

Also, I understand that doing the above helps the compiler because it can easily determine the types and number of the arguments / parameters right at the start. It then uses this information to check the function definition and function call. All 3 of these things must match.

If one puts the function declarations and definitions in the order in which they are called (where possible), then any unused functions will be at the bottom of the file.




If you are talking about C++ code with classes , then that is a bit different. The class declarations go into a header file (I prefer .hpp for c++, .h for C), while the implementation code for the functions goes into a .cpp file. Name the files exactly the same as the class name. Prefer not to have implementation code inside your class declaration, unless it is really small - 2 or 3 lines.

The reason why we do things like this, is primarily for good organisation. One #include 's only the header files for the classes being used. It also means that one can use that class in multiple places in the project. Note that one should never #include a .cpp file.

Another handy concept is to put all your own code into 1 or more of your own namespaces. This is to avoid naming conflicts on larger projects. I always do it - regardless of the size of the code.

Hope all this helps a little bit, and that all is well at your end :+)

WOW, thanks for all the great information, it directs me to other items I need to read up on, well thought out information, thanks so much.
I can think of no reason to put them after main().

appearances. in a single source file its convienent to be able to see a declaration of functions and jump right into main.
It doesn't make a difference to the compiler. What it's really about is readability for the programmer. If someone is looking at your code for the first time, it's nice to have prototypes near the top with brief comments on what the functions do. That way they don't have to devine the overall structure of your code.
Topic archived. No new replies allowed.