简体   繁体   中英

What is the best practice between exit() and return in function in C

Could you tell me if it is better to use in this example exit() in a function or rather return please?

#include <stdio.h>
#include <stdlib.h>

void function(char *);
int function2(char *);

int main(void)
{
        char *p;
        function(p);
        if(function2(p) < 0)
                exit(EXIT_FAILURE);

        return EXIT_SUCCESS;
}

void function(char *p)
{
        if(p == NULL)
                exit(EXIT_FAILURE);
}

int function2(char *p)
{
        if(p == NULL)
                return -1;
        return 0;
}

I find that using return when you have several nested functions makes the code less readable since you have to go back to main to exit.

However I find that it is easier to test its functions in unit tests with returns.

I find that using return when you have several nested functions makes the code less readable since you have to go back to main to exit.

However I find that it is easier to test its functions in unit tests with returns.

Yes, there are conflicting priorities involved, and those two are not the only ones. For example, if the function in question is intended for a reusable library, then exiting the program on detecting an error condition probably is not a viable behavior. On the other hand, if a need for immediate, unconditional termination is detected in a multithreaded program, then exit() is among the cleanest available ways to achieve it. The list goes on from there.

However, exit() ing is not recoverable, so any function that does it must be darn sure that it's the right thing to do under the circumstances. Thus, I'm inclined to say that although the best approach varies from case to case, it is strongly biased toward returning an error indicator or signaling an error in some other documented way, rather than calling exit() or another function ( _exit() , abort() , ...) that terminates the program.

Your question ("What is the best practice between exit() and return in function in C") is very general, and an answer would either be "it depends on..." or it would be a tutorial on C functions.

However, looking at your example, and the fact that you tagged the question with "pointers", makes me think that you really want to know how functions should handle invalid input.

Do not use exit for invalid input, there are better alternatives. Consider using assert or exceptions (if your compiler supports exceptions), both of these choices will give you information on what the error was and where it occurred.

I can't give you a complete answer off the top of my head, but my approach to handling errors (including invalid input) is usually to do this:

If the error can only be produced by a bug in the program then
 Report the error giving as much information as possible to help fix the bug,
 and then stop the program.
Else
 The function that detected the error should, depending on the error,
 either return an error code to the function that called it
 or treat the error as if it was produced by a bug.
 When a function calls a function, and gets an error code, it can, depending on the error code,
 either handle the error
 or treat the error as if it was produced by a bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM