Thread: function exit with out return statement

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    4

    function exit with out return statement

    hello everybody,

    Is the following code valid in c.

    // if "a" is false the function "check" will exit with out a return.
    Code:
    BOOL check(BOOL a, int b)
    {
         int val;
         
         if (a)
           return TRUE;
    
         else
            val =b;
    }

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    You will get a compiler warning or error if you try to do that.

    You've told it that you will give an answer and then given none.

    return FALSE, or don't use BOOL and return some over value that means "I can't give an answer" (e.g. use int instead and then return: 0 = FALSE, 1 = TRUE, 2 = I can't give an answer).

    Or, to be honest, just restructure how that function is supposed to work (i.e. at the moment, it's pointless - you're just returning a, or setting an internal variable (which gets immediately destroyed on function exit) to b.

    If you don't care about the return, don't check it from calling functions.
    If you don't know what to return, wonder why you bother to have the function.
    If you have circumstances where you *can't* return what you want, change the way the return value works.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Hello ledow,

    Thank u for the reply and the recommendations. I was just curious that how c takles this issue. Actually "check" function (a dummy example for illustration) is called several times. The calling function cares about the returned value only the first time (when a= TRUE). In the latter calls (a=FALSE) and my calling function also does not care about the value retuned by the "check". So in this case my else will be executed which has no return statement so what will be the response of c and will it effect the latter steps of my program.

    thanks

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    How "c tackles this issue" is that a function is not required to return a value, even if it has a non-void return type. So your code will compile, albeit with warnings (both to the effect of "return with no value" due to falling off the end the function, and val being assigned a value that is never used).

    The problem comes for the caller. If the caller uses the return value, but the function has fallen off the end, then the caller exhibits undefined behaviour. That means anything is allowed to happen (eg a program crash, caller receiving a value of zero) at that point.


    Note that the main() function is special: falling off the end is equivalent to it returning zero.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-14-2012, 09:26 AM
  2. function - return statement
    By chungt004 in forum C Programming
    Replies: 5
    Last Post: 02-27-2008, 08:33 PM
  3. Isn't "return" supposed to exit a function?
    By cunnus88 in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2008, 04:58 AM
  4. exit() vs return()
    By kryptkat in forum C Programming
    Replies: 9
    Last Post: 10-21-2006, 11:26 AM
  5. Exit with Return
    By Aidman in forum Linux Programming
    Replies: 6
    Last Post: 12-28-2005, 10:17 AM