return 0 to main from within a function, is this possible?

This is a discussion on return 0 to main from within a function, is this possible? within the C++ Programming forums, part of the General Programming Boards category; For example; Code: int func (int someval){ if (someval == 3) { // make int main return 0; } return ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    106

    return 0 to main from within a function, is this possible?

    For example;

    Code:
    int func (int someval){
      if (someval == 3) {
       // make int main return 0;
      }
      return 0;
    }
    
    int main (){
      for (int i = 0 ; i < 10 ; i++){
        func(i);
      }
      return 0;
    }
    How can you return 0 to main, from within another function? Is there a standard way of doing this, or will I have to make somefunc return some special value that gets checked by an if statement within main and then that will return 0 to main?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    You can use the exit() function, which quits your program with the return code provided as the first parameter. But it's use is generally to be avoided because it messes up code flow.

    It is generally better to use a special return value or an exception. Or restructure your code such that you use an object to store state, and have the return value be solely as an indicator of what to do next.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,265
    sorry to go off topic, but I had to say, King Mir, that I love your sig. Reminds me of what Oma Desala said to Daniel Jackson:

    "Because it is so clear it takes a long time to realize it. If you immediately know the candlelight is fire, the meal was cooked a long time ago."

    My favorites will always be:

    "The success or failure of your deeds does not add up to the sum of your life. Your spirit cannot be weighed. Judge yourself by the intention of your actions and by the strength with which you faced the challenges that have stood in your way."

    and

    "The universe is vast and we are so small. There is only one thing we can ever truly control. Whether we are good or evil."

    they are very similar to certain Buddhist sayings, which I'm guessing is the source of your sig, and while they were spoken by a fictional character, that does not diminish the truth that they contain.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by elninio View Post
    For example;

    Code:
    int func (int someval){
      if (someval == 3) {
       // make int main return 0;
      }
      return 0;
    }
    
    int main (){
      for (int i = 0 ; i < 10 ; i++){
        func(i);
      }
      return 0;
    }

    Code:
    int func (int someval){
      if (someval == 3) {
       return 1;
      }
      return 0;
    }
    
    int main (){
      for (int i = 0 ; i < 10 ; i++){
        if ( func(i) ) {return 0;}
      }
       return 0;
    }
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Quote Originally Posted by elninio View Post
    or will I have to make somefunc return some special value that gets checked by an if statement within main and then that will return 0 to main?
    Yes that's pretty much what you have to do.

    Comedy option: use a goto statement

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by Neo1 View Post
    Code:
    int func (int someval){
      if (someval == 3) {
       return 1;
      }
      return 0;
    }
    
    int main (){
      for (int i = 0 ; i < 10 ; i++){
        if ( func(i) ) {return 0;}
      }
       return 0;
    }
    func should probably return bool in this example, however. Or an enum, which main will then check.
    Returning an integer and comparing it as bool is more C-style than C++ since there is a dedicated type for it...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    Quote Originally Posted by Elkvis View Post
    sorry to go off topic, but I had to say, King Mir, that I love your sig. Reminds me of what Oma Desala said to Daniel Jackson:

    "Because it is so clear it takes a long time to realize it. If you immediately know the candlelight is fire, the meal was cooked a long time ago."

    My favorites will always be:

    "The success or failure of your deeds does not add up to the sum of your life. Your spirit cannot be weighed. Judge yourself by the intention of your actions and by the strength with which you faced the challenges that have stood in your way."

    and

    "The universe is vast and we are so small. There is only one thing we can ever truly control. Whether we are good or evil."

    they are very similar to certain Buddhist sayings, which I'm guessing is the source of your sig, and while they were spoken by a fictional character, that does not diminish the truth that they contain.
    My sig is indeed from a Budhist text called the The Gateless Gate. I believe it to be the original source of the SG1 quote. Stargate SG1 made a point of this poem being hard to understand, but it seems the problem was in translation and context. Yet in it's first appearance in the series the context was made clear by the next line which is given here (this is the original Zen version):
    Lightning flashes,
    Sparks shower.
    In one blink of your eyes
    You have missed seeing.
    Last edited by King Mir; 09-23-2008 at 07:36 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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