Tuesday 8 November 2011

Should a function contain a return statement if it does not return a value? in C programming

Should a function contain a return statement if it does not return a value?

In C, void functions (those that do not return a value to the calling function) are not required to include a
return statement. Therefore, it is not necessary to include a return statement in your functions declared as
being void.

In some cases, your function might trigger some critical error, and an immediate exit from the function might
be necessary. In this case, it is perfectly acceptable to use a return statement to bypass the rest of the function’s

code. However, keep in mind that it is not considered good programming practice to litter your functions
with return statements—generally, you should keep your function’s exit point as focused and clean
as possible.

Cross Reference:

VIII.8: What does a function declared as PASCAL do differently?
VIII.9: Is using exit() the same as using return?

No comments:

Post a Comment