Using return

suggest change

Returning a value

One commonly used case: returning from main()

#include <stdlib.h> /* for EXIT_xxx macros */

int main(int argc, char ** argv)
  if (2 < argc)
    return EXIT_FAILURE; /* The code expects one argument: 
                            leave immediately skipping the rest of the function's code */

  /* Do stuff. */

  return EXIT_SUCCESS;

Additional notes:

  1. For a function having a return type as void (not including void * or related types), the return statement should not have any associated expression; i.e, the only allowed return statement would be return;.
  2. For a function having a non-void return type, the return statement shall not appear without an expression.
  3. For main() (and only for main()), an explicit return statement is not required (in C99 or later). If the execution reaches the terminating \}, an implicit value of 0 is returned. Some people think omitting this return is bad practice; others actively suggest leaving it out.

Returning nothing

Returning from a void function

void log(const char * message_to_log)
  if (NULL == message_to_log)
    return; /* Nothing to log, go home NOW, skip the logging. */

  fprintf(stderr, "%s:%d %s\n", __FILE__, _LINE__, message_to_log);

  return; /* Optional, as this function does not return a value. */

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents