CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2014
    Posts
    28

    In what instance would Main() return a value?

    According to MS, Main() can return a value: http://msdn.microsoft.com/en-us/library/k1sx6ed2.aspx

    I'm trying to think of an instance when Main() would need to return a value, being that it's the main program control.

    Is it for cases where main may be used iteratively, calling itself multiple times? That, in itself, seems like it would be bad coding design.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: In what instance would Main() return a value?

    Think of a console app utility program. Most beginners make the mistake of creating a utility program that simply outputs text to the console for success or failure.

    However, if you call the utility app via scripting or a batch file, it's a headache to have to pipe and parse the text to determine if the utility succeeded.

    A better way is to always return an error code (or 0 for success) along with the text. Then the script or batch file can simply check the utility's return value.

  3. #3
    Join Date
    Apr 2014
    Posts
    28

    Re: In what instance would Main() return a value?

    Quote Originally Posted by Arjay View Post
    Think of a console app utility program. Most beginners make the mistake of creating a utility program that simply outputs text to the console for success or failure.

    However, if you call the utility app via scripting or a batch file, it's a headache to have to pipe and parse the text to determine if the utility succeeded.

    A better way is to always return an error code (or 0 for success) along with the text. Then the script or batch file can simply check the utility's return value.
    Ah. I'd always thought the value was returned via console. This makes sense.

  4. #4
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: In what instance would Main() return a value?

    here is a example on the msdn
    http://msdn.microsoft.com/en-us/library/0fwzzxz2.aspx
    it could just be in a app that starts another process or exe as well
    you might want to get the error code if it failed, then toss a message to the user.
    "like error code -1 the application failed to load the file the file maybe corrupt"

    I'm trying to think of an instance when Main() would need to return a value, being that it's the main program control.
    os executes your program (the os needs to know if it has exited gracefully or crashed to send a messege)
    Last edited by willmotil; June 3rd, 2014 at 01:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured