Thread: Exitting a void 'function' ?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Exitting a void 'function' ?

    Hello all...

    Ive tried searchin the boards, but without much luck (mainly because peoples thread titles tend to suck at descriping what they are all about )

    Well, my problem is this:

    In a program I have to call a void 'function' (it's not really a function, but whats the other word? Procedure in VB ). In this 'function' I need to exit it at a certain point. Like:

    if (cond)
    exit void;

    .. I don't know if this is possible though? I can't make it into an int function and just return 0 btw. Can anyone give me a hint?

    Thx in advance

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    For a function with a 'return type' of 'void' just use 'return' if you need to; eg
    Code:
    int FunctionThatReturnsInt()
    {
    int the_answer=42;
    return the_answer;
    }
    
    void FunctionThatDoesNotReturnAnything()
    {
    if (some_condition_is_met)
      {
      return;
      }
    //function returns anyway
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it's not really a function, but whats the other word?
    Procedure. But in C++, a function is a function, even if it doesn't have a return value.

    >Can anyone give me a hint?
    You can use return without an operand.
    Code:
    void foo()
    {
      // stuff
    
      if ( condition )
        return;
    
      // more stuff
    }
    My best code is written with the delete key.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    you could also only branch on the not true condition, and do nothing otherwise

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    hehe, can't believe I didnt try that myself xD

    Thx ppl

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Using return without operand will give you a compiler error on MSVC++ 2003. It's worth checking if it's legal as I know that the 2003 edition is pretty compliant to the standards.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Using return without operand will give you a compiler error on MSVC++ 2003. It's worth checking if it's legal as I know that the 2003 edition is pretty compliant to the standards.

    It is completely legal and works fine in VC++ 2003. Perhaps you are thinking of using return; in a function that doesn't specify void as its return type.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Daved
    >> Using return without operand will give you a compiler error on MSVC++ 2003. It's worth checking if it's legal as I know that the 2003 edition is pretty compliant to the standards.

    It is completely legal and works fine in VC++ 2003. Perhaps you are thinking of using return; in a function that doesn't specify void as its return type.
    Yes he MUST be!

    Slap on the wrist to Desolation for not reading entire thread and engaging brain before posting

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM