Thread: Which functions come first?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    10

    Which functions come first?

    I just realized that if you have a function which calls another funtion, the one it calls must be written in the code prior to the calling function so that the compiler knows what you're looking for.

    Why is that? What if it gets to be a mess of arranging functions in a proper order, or what if it's impossible?

    Is there some better way of handling this?
    chaser

  2. #2
    Registered User tgm's Avatar
    Join Date
    Jun 2002
    Posts
    150
    Write function declarations at the top (or in a header file):

    int function( int, float );

    then later in the file write the definitions (or in a separate file):

    int function( int foo, float bar )
    {
    /* function code */
    }

    This way is doesn't matter what order the functions are defined in.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    10

    Thanks

    I had just read that in the tutorial after I posted the question. Thanks anyhow, I guess it's called Prototyping

    I've got all my functions done that way now, I suppose it's a good habbit.
    chaser

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    I'll admit I still forget to prototype lots of stuff sometimes

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The reason you need to prototype or declare functions before you use them is because the compiler needs to know what a function is before you can use it. You would think by now you wouldn't need to do this. Look at JAVA.

  6. #6
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Try switching from JavaScript to C++! You don't prototype in JS, so when i made the switch I pulled my hair out at all these c++ cause I thought they were all wrong! *bonks self* Now JavaScript looks funny to me. No semi-colons? WTF
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    I was the same way... before C++ I was heavily into JavaScript, which is why I still forget sometimes to prototype

    prototyping is a result of C++'s type-checking and function overloading

  8. #8
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    heh i guess i'm going in the opposite direction than you guys i started with c and now i'm trying to learn java (applets and applications), but i havn't tried javascript so i dont know if its the same.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    208

    Well???

    I started with a little javascript then java then I moved on to C++ and next came PERL. I dunno why but fer sum reason I can't just learn one thing at a time. Oh well I guess I mostly program C++ though

  10. #10
    >>
    Why is that? What if it gets to be a mess of arranging functions in a proper order, or what if it's impossible?
    >>Is there some better way of handling this?

    Oh yes, most definatly. Much better than prototypes: Take advantage of C++ and use Classes. You'll never regret it.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  11. #11

    I love classes

    I do love classes. I learned Javascript, then basic, then JAVA, then i learned C. When i got a C book i almost screamed because i wanted to make classes, not wimpy structs. Then i got C++ into my brain, and classes dominate 2/3 of my header files. YES!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM