question about c++

Discussion in 'OT Technology' started by Corp, Feb 22, 2005.

  1. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    I'm learning c++ on my own, and i was just wondering why instead of having main return an int, why not just have it be a void function? Doesn't seem necessary to return anything from main.
     
  2. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    Main should return an integer so the parent process will know if it exited abnormally.
     
  3. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    I've heard that before, but what is it gonna do if it's just void?
     
  4. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    Nothing bad will happen. Some strict compilers will throw an error if main is not an int or if it has no return value. I'm pretty sure gnu's gcc doesn't care.
     
  5. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    when you're starting out with basic standalone applications, void is fine. In the future, you may find returning a value to be useful (if not required), however.

    But why bother with void instead of int? You still have to "return"... so instead of "return 1;" you "return;".... Wow! You saved 2 bytes!
     
  6. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    why would you have to return for a void function?
     
  7. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    all functions should have a return... Some compilers wont even compile without it.
     
  8. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    oh, i only know java so :dunno:
     
  9. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    http://www.eskimo.com/~scs/readings/voidmain.960823.html
    Also, no need for void functions to return something. If your function just displays some text on the screen, or initializes values, returns are not necessary. Usually, int is used in the main to tell the operating system that it returned successfully. Also, sometimes, we may want to tell the operating system that an error occurred.

    Just found a better answer for you:
    http://www.research.att.com/~bs/bs_faq2.html#void-main
     
    Last edited: Feb 23, 2005

Share This Page