Thread: Style

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428

    Style

    I've noticed alot of the more experienced coders when they post code with functions it looks like.

    Code:
    #include <iostream.h>
    
    using namespace std;
    
    int getint();
    
    int main (int argc, char *argv[])
    {
      int c;
      c = getint();
      cout <<c<<"\n";
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    
    int getint()
    {
      int c;
      cin>> c;
      return c;
    }
    I usually write my programs like this
    Code:
    #include <iostream.h>
    
    using namespace std;
    
    int getint()
    {
      int c;
      cin>> c;
      return c;
    }
    
    int main (int argc, char *argv[])
    {
      int c;
      c = getint();
      cout <<c<<"\n";
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    I know it is a small difference, but I find small differences usually turn to big differences later. Can anyone explain why they declare their functions on top of main and then define them underneath? In my high school computer programming class (7 yrs ago) my teacher had us declare and define above the statement of int main. Does this maybe keep functions from needing to be defined in a certain order? I'm sure this is a beginner question, but thanks for your time.

    Ohh and why:
    int main (int argc, char *argv[])

    7 yrs ago I was taught
    int main()
    Last edited by Lesshardtofind; 07-22-2009 at 11:46 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your style will instantly break once you try to use more than one source code file (whether because you just have a lot of functions, or because you are on a team of > 1 person, or because you are reusing some functions from last year sometime). -- Well, you'll have to create the prototypes then, at least.

    And as to your new question: if you intend to use the command line arguments, you must give them a name in order to use them. argc and argv are customary, but you can call them whatever.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your style will instantly break once you try to use more than one source code file
    How is anything he posted dependent on the number of source files involved? If you have source files which call functions in other source files, that's where headers come into play. It has nothing to do with the order of the functions in a particular source file.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bithub View Post
    How is anything he posted dependent on the number of source files involved? If you have source files which call functions in other source files, that's where headers come into play. It has nothing to do with the order of the functions in a particular source file.
    If your style is "I don't have any prototypes", that makes it hard to create a header file out of your function prototypes (given that they do not exist). Obviously you will have to create them at that time. (And also, the prototypes must be included before the functions they declare can be called.)

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    eh, I didn't get "I don't have any prototypes" out of that. I think it's just about the function order in a .cpp file.

    In that case, the only thing that will break his style (not prototypes at the beginning of source files) is something like:
    Code:
    void foo(void)
    {
        if(bar_condition)
            bar();
    }
    
    void bar(void)
    {
        if(foo_condition)
            foo();
    }

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    ok firstly you are saying the declaration
    int getint();
    at the top is known as a prototype correct?

    Second you said it does affect when a function can be called based on when it is declared in code and prototypes will save me from having to define one before the other?

    Third I don't see why it would matter with including other source files. I could just declare all the functions in the correct order in the source file then #include <source.h> correct? Assuming the source is named source.h and located in my include folder.

    Not having to worry about the order that is needed to define functions is useful, because in my current style I find myself having to rearrange my function order all the time, and I've thought it seemed weak to have a program depend on the order.

    I am a solo programmer and just am teaching myself through programming games and reading posts here, so at the moment the programming something >1 programmers does not apply, but will be kept in mind.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    ok firstly you are saying the declaration
    int getint();
    at the top is known as a prototype correct?
    yes

    Second you said it does affect when a function can be called based on when it is declared in code and prototypes will save me from having to define one before the other?
    correct, a function must be declared as a prototype or implemented before it can be called. In other words:
    Code:
    void foo(void)
    {
        /* Calling bar() here is illegal since it is implemented below instead of above.  
         * We can fix this by moving bar's implementation up above foo's, or we can just
         * add a prototype for bar() up above. */
        bar();
    }
    void bar(void)
    {
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Lesshardtofind View Post
    ok firstly you are saying the declaration
    int getint();
    at the top is known as a prototype correct?

    Second you said it does affect when a function can be called based on when it is declared in code and prototypes will save me from having to define one before the other?
    Right. No function can be called unless it has already been either defined (as you've been doing it) or declared (with a prototype).
    Quote Originally Posted by Lesshardtofind View Post
    Third I don't see why it would matter with including other source files. I could just declare all the functions in the correct order in the source file then #include <source.h> correct? Assuming the source is named source.h and located in my include folder.
    Since a .h file consists of essentially nothing but prototypes, it allows you to use the functions in that other source code file. (Note that other source code file is most definitely NOT NOT NOT #included in your file, it is compiled separately and linked together.) Therefore the order of the functions in the file itself is then completely irrelevant.

  9. #9
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Quote Originally Posted by bithub View Post
    eh, I didn't get "I don't have any prototypes" out of that. I think it's just about the function order in a .cpp file.

    In that case, the only thing that will break his style (not prototypes at the beginning of source files) is something like:
    Ok good point, guess I will change it now. I am making a rpg in C++ using SDL and the code is starting to get big and annoying to figure out where each function is when I think of a new function. (by the way I know I program sloppy because I just get an idea and wing it rather than brainstorming all the possible functions and subfunctions beforehand, anyone have good methods of pre-planning or at least during project documentation that cuts down on the confusion?) The bigger the program gets the harder it gets to find infinite loops and bugs that I accidently program in.

  10. #10
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    In the end you will be using headers with prototypes so it doesn't really matter. If I had to choose the style for smaller program I'd pick the style that doesn't require pre-prototyping.

    By the way do you write parameter names in the headers too? I don't when I'm not writing on libs.

  11. #11
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Quote Originally Posted by tabstop View Post
    Since a .h file consists of essentially nothing but prototypes, it allows you to use the functions in that other source code file. (Note that other source code file is most definitely NOT NOT NOT #included in your file, it is compiled separately and linked together.) Therefore the order of the functions in the file itself is then completely irrelevant.
    I was not aware of this. So you would #include the source.h in your main file and #include the source.cpp in your .h? (or would you include both in main and include the source.h in the source.cpp...I think that makes more since). Then in the .h purely declare all your prototype functions and in the .cpp define all of those functions?

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    By the way do you write parameter names in the headers too? I don't when I'm not writing on libs.
    They can be handy if you have an IDE which displays the entire prototype when doing auto-complete.

    and #include the source.cpp in your .h?
    No. Never ever #include a source file.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Lesshardtofind View Post
    I was not aware of this. So you would #include the source.h in your main file and #include the source.cpp in your .h? (or would you include both in main and include the source.h in the source.cpp...I think that makes more since). Then in the .h purely declare all your prototype functions and in the .cpp define all of those functions?
    Neither. Your source.cpp file will be compiled separately and in no way is compiled when your main.cpp is compiled. When everything is linked together, the function calls you made to functions in source.cpp are then resolved. (For instance, I'm sure you've used printf() before, but I imagine you've never written a printf function. (I haven't either.) You don't need to have source for printf -- when your executable is created, during the linking process, the printf function is found in the runtime library and either the code is included in your executable or a bit of code is put in to use the appropriate DLL.)

  14. #14
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Quote Originally Posted by Tux0r View Post
    By the way do you write parameter names in the headers too? I don't when I'm not writing on libs.
    I'm guessing you mean declaring int in the header rather than in the function its used in?. I declare variables in the header only if I want it to be global and variables that I only need for a function stay local. This allows me to have functions that manipulate 3 or 4 variables in the game and are voids with no return. If I answered the wrong question I'm sorry I'm not sure what parameter names means lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM