简体   繁体   中英

return statement in void function in C

In the following program, Why compiler does not gives any error or warning?

cc -Wall -pedantic my_program.c

code here:

#include <stdio.h>

void f()
{
        return; // return statement in void function
}

int main()
{
        f();
        return 0;
}

I have compiled the program in GCC compiler on Linux platform.

Of course you can use return; in a void function. How else would you return early from such a function?

In your case the statement is redundant as it's reached on all control paths, but a compiler will certainly not issue a diagnostic in that instance.

(Note that you must return explicitly from a non- void function, with the exception of main() , where a compiler must generate return 0; if missing.

A void function will perform the task, and then control returns back to the caller but, it's not going to return a value. There isn't any value to return.

Without the return statement, control will return to the caller at the end of the function anyways.

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