Defining functions below int main() - Best practices?

This is a discussion on Defining functions below int main() - Best practices? within the C++ Programming forums, part of the General Programming Boards category; I don't understand why one would define some functions below int main()...??? What is the point of that exactly? It ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Defining functions below int main() - Best practices?

    I don't understand why one would define some functions below int main()...???

    What is the point of that exactly?

    It would seem to me that the best practice would be to have int main() at the bottom of all code. Why would you write code beneath the main() function?

    Also, in a book entitled "Practical C++" by Robert W. McGregor, in example code, the main() function always has the return type void instead of int. Why is this? I thought the main function always has to be int? Or does it just depend on the compiler/platform?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Some people consider it to be clearer. Program control flow starts at main, and logically people like to read downward. It's not that big of a deal either way.

    As for main, old, pre-standard compilers, programs and books often used void instead of int. It's not a big deal, either, but the standard states that it should be int so that the calling programs can expect a non-random return value. If your book uses void main, it might be worth considering a more modern or better book, but if you continue to use it just switch to int for main.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    On the projects I've worked on where this was even an issue in the coding guidelines, it was always specified that main() should be the first function in the file it appears in, and preferably that it be the ONLY function in that file. I've never seen it suggested that main() should be at the bottom of the file.

    At any rate, arguing over whether a function is above or below main() is sort of like arguing whether the Milky Way is to the left or right of the Andromeda galaxy
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    I don't know...
    I just find it easier to have main() at the bottom of the code, seeing as you if you choose to define functions below the main() function, you still have to declare them above it, so what's the point?

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by Programmer_P View Post
    I don't know...
    I just find it easier to have main() at the bottom of the code, seeing as you if you choose to define functions below the main() function, you still have to declare them above it, so what's the point?
    That means you're choosing where to place functions in relation to other functions because you don't want to write prototypes. The functions should be listed in some kind of logical order, not whatever order happens to be compilable.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,453
    Quote Originally Posted by Programmer_P
    I just find it easier to have main() at the bottom of the code, seeing as you if you choose to define functions below the main() function, you still have to declare them above it, so what's the point?
    To make moving those prototypes to a header file easier since you have already written them
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by brewbuck View Post
    That means you're choosing where to place functions in relation to other functions because you don't want to write prototypes. The functions should be listed in some kind of logical order, not whatever order happens to be compilable.
    No, correction: I'm still writing prototypes of functions (which, as I understand it, has to be done), just I have both the prototypes and the definitions above int main, because it makes it easier to read that way.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    To make moving those prototypes to a header file easier since you have already written them
    Huh?? Sorry, I'm just not seeing the connection...
    How would defining functions beneath int main() make it "easier to move the prototypes to a header file". That doesn't make much sense...
    Last edited by Programmer_P; 12-09-2009 at 10:31 AM. Reason: use italics for "easier"

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,453
    Quote Originally Posted by Programmer_P
    No, correction: I'm still writing prototypes of functions (which, as I understand it, has to be done)
    It is not always the case that prototypes have to be provided, but usually they are, unless the function is defined inline.

    Quote Originally Posted by Programmer_P
    just I have both the prototypes and the definitions above int main, because it makes it easier to read that way.
    More accurately, because you find it easier to read that way. As Daved pointed out, some people find it easier to read with the definition of the global main function at the top. Do what you will, but follow the rules if a project that you are working on mandates it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Cool

    Quote Originally Posted by laserlight View Post
    More accurately, because you find it easier to read that way. As Daved pointed out, some people find it easier to read with the definition of the global main function at the top. Do what you will, but follow the rules if a project that you are working on mandates it.
    Affirmative. At least we can agree there.
    If I was on a multiple-programmer project, and I wasn't the one who started and was running the project, I would indeed follow whatever rules the Head Honcho defined.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by Programmer_P View Post
    Affirmative. At least we can agree there.
    If I was on a multiple-programmer project, and I wasn't the one who started and was running the project, I would indeed follow whatever rules the Head Honcho defined.
    If you don't put any other functions at all in the same file with main(), then nobody can argue with you no matter what their opinion
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Here is the deal.

    main() must return an int for obvious reasons already explained.

    As far as functions being below main(), is, as explained, governed by project coding rules or up to the lead developer. Whats your technique then? There really is no wrong way, but more of how readable is your code and helps to strengthen self documentation.

    If you provide function declarations, then the function definitions can be below main().

    - By providing function declaration before main(), provides more of an OOP approach of which you can move those function declarations to another header file for easier manageability/code modifications latter.

    - Also as the project grows in size or if more than 2 programmers need to compile in changes, then moving your functions to another header file, again, adds to the OOPness of the project.

    - If everything must be at the top of main(), then your declares and definitions must be provided as this helps the compiler.(Thats my understanding)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,453
    Quote Originally Posted by slingerland3g
    - By providing function declaration before main(), provides more of an OOP approach of which you can move those function declarations to another header file for easier manageability/code modifications latter.

    - Also as the project grows in size or if more than 2 programmers need to compile in changes, then moving your functions to another header file, again, adds to the OOPness of the project.
    Personally, I feel that "modular" and "modularity" should be used instead of "OOP" and "OOPness" in this context. I do not see anything particularly object oriented about moving forward declarations to a header file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by slingerland3g View Post
    Here is the deal.

    main() must return an int for obvious reasons already explained.

    As far as functions being below main(), is, as explained, governed by project coding rules or up to the lead developer. Whats your technique then? There really is no wrong way, but more of how readable is your code and helps to strengthen self documentation.

    If you provide function declarations, then the function definitions can be below main().

    - By providing function declaration before main(), provides more of an OOP approach of which you can move those function declarations to another header file for easier manageability/code modifications latter.

    - Also as the project grows in size or if more than 2 programmers need to compile in changes, then moving your functions to another header file, again, adds to the OOPness of the project.

    - If everything must be at the top of main(), then your declares and definitions must be provided as this helps the compiler.(Thats my understanding)
    True, you provide some good points. The truth is, I had not thought about putting all functions other than main() in header file(s) until it was suggested in this thread, and so it seemed to me the best way to do it would be to put both declarations and definitions above int main. But, yeah, I can easily see how putting all function declarations (I'm assuming you can't put the definitions in too?) in header files, with the definitions below int main() would improve the readability of the whole code.

  15. #15
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by laserlight View Post
    Personally, I feel that "modular" and "modularity" should be used instead of "OOP" and "OOPness" in this context. I do not see anything particularly object oriented about moving forward declarations to a header file.
    True, yet from time to time in analyzing code I do see separate header files for just declarations only, the modularity of such an implementation of header file madness I am not sure on.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21